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,
976 // so that it can finish all clean-up and return the correct exit
977 // code to the OS.
978 } else {
979 // The JLI launcher only recognizes the "old" -Xshare:dump flag.
980 // When the new -XX:AOTMode=create flag is used, we can't return
981 // to the JLI launcher, as the launcher will fail when trying to
982 // run the main class, which is not what we want.
983 struct stat st;
984 if (os::stat(AOTCache, &st) != 0) {
985 tty->print_cr("AOTCache creation failed: %s", AOTCache);
986 } else {
987 tty->print_cr("AOTCache creation is complete: %s " INT64_FORMAT " bytes", AOTCache, (int64_t)(st.st_size));
988 }
989 vm_direct_exit(0);
990 }
991 }
992 }
993
994 #if INCLUDE_CDS_JAVA_HEAP && defined(_LP64)
995 void AOTMetaspace::adjust_heap_sizes_for_dumping() {
996 if (!CDSConfig::is_dumping_heap() || UseCompressedOops) {
997 return;
998 }
999 // CDS heap dumping requires all string oops to have an offset
1000 // from the heap bottom that can be encoded in 32-bit.
1001 julong max_heap_size = (julong)(4 * G);
1002
1003 if (MinHeapSize > max_heap_size) {
1004 log_debug(aot)("Setting MinHeapSize to 4G for CDS dumping, original size = %zuM", MinHeapSize/M);
1005 FLAG_SET_ERGO(MinHeapSize, max_heap_size);
1006 }
1007 if (InitialHeapSize > max_heap_size) {
1008 log_debug(aot)("Setting InitialHeapSize to 4G for CDS dumping, original size = %zuM", InitialHeapSize/M);
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::prepare_for_archiving(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,
987 // so that it can finish all clean-up and return the correct exit
988 // code to the OS.
989 } else {
990 // The JLI launcher only recognizes the "old" -Xshare:dump flag.
991 // When the new -XX:AOTMode=create flag is used, we can't return
992 // to the JLI launcher, as the launcher will fail when trying to
993 // run the main class, which is not what we want.
994 struct stat st;
995 if (os::stat(AOTCache, &st) != 0) {
996 tty->print_cr("AOTCache creation failed: %s", AOTCache);
997 } else {
998 tty->print_cr("AOTCache creation is complete: %s " INT64_FORMAT " bytes", AOTCache, (int64_t)(st.st_size));
999 }
1000 print_statistics_before_exit();
1001 ostream_exit(); // finalize VM log
1002 vm_direct_exit(0);
1003 }
1004 }
1005 }
1006
1007 #if INCLUDE_CDS_JAVA_HEAP && defined(_LP64)
1008 void AOTMetaspace::adjust_heap_sizes_for_dumping() {
1009 if (!CDSConfig::is_dumping_heap() || UseCompressedOops) {
1010 return;
1011 }
1012 // CDS heap dumping requires all string oops to have an offset
1013 // from the heap bottom that can be encoded in 32-bit.
1014 julong max_heap_size = (julong)(4 * G);
1015
1016 if (MinHeapSize > max_heap_size) {
1017 log_debug(aot)("Setting MinHeapSize to 4G for CDS dumping, original size = %zuM", MinHeapSize/M);
1018 FLAG_SET_ERGO(MinHeapSize, max_heap_size);
1019 }
1020 if (InitialHeapSize > max_heap_size) {
1021 log_debug(aot)("Setting InitialHeapSize to 4G for CDS dumping, original size = %zuM", InitialHeapSize/M);
1147 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1148 // fails verification, all other interfaces that were not specified in the classlist but
1149 // are implemented by K are not verified.
1150 link_shared_classes(CHECK);
1151 log_info(aot)("Rewriting and linking classes: done");
1152 TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
1153
1154 if (CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
1155 LambdaFormInvokers::regenerate_holder_classes(CHECK);
1156 }
1157
1158 #if INCLUDE_CDS_JAVA_HEAP
1159 if (CDSConfig::is_dumping_heap()) {
1160 HeapShared::init_heap_writer();
1161 if (CDSConfig::is_dumping_full_module_graph()) {
1162 ClassLoaderDataShared::ensure_module_entry_tables_exist();
1163 ClassLoaderDataShared::build_tables(CHECK);
1164 HeapShared::prepare_for_archiving(CHECK);
1165 }
1166
1167 if (ArchiveLoaderLookupCache) {
1168 SystemDictionaryShared::create_loader_positive_lookup_cache(CHECK);
1169 }
1170
1171 AOTReferenceObjSupport::initialize(CHECK);
1172 AOTReferenceObjSupport::stabilize_cached_reference_objects(CHECK);
1173
1174 if (CDSConfig::is_dumping_aot_linked_classes()) {
1175 // java.lang.Class::reflectionFactory cannot be archived yet. We set this field
1176 // to null, and it will be initialized again at runtime.
1177 log_debug(aot)("Resetting Class::reflectionFactory");
1178 TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
1179 Symbol* method_sig = vmSymbols::void_method_signature();
1180 JavaValue result(T_VOID);
1181 JavaCalls::call_static(&result, vmClasses::Class_klass(),
1182 method_name, method_sig, CHECK);
1183
1184 // Perhaps there is a way to avoid hard-coding these names here.
1185 // See discussion in JDK-8342481.
1186 }
1187 } else {
1188 log_info(aot)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling");
1189 CDSConfig::stop_using_optimized_module_handling();
1190 }
1191 #endif
1192
1193 if (!CDSConfig::is_dumping_preimage_static_archive()) {
1194 if (CDSConfig::is_dumping_final_static_archive()) {
1195 FileMapInfo::free_current_info(); // FIXME: should not free current info
1196 }
1197 open_output_mapinfo();
1198 }
1199
1200 VM_PopulateDumpSharedSpace op(builder, _output_mapinfo);
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, _output_mapinfo, op.mapped_heap_info(), op.streamed_heap_info());
1229 assert(!_output_mapinfo->is_open(), "Must be closed already");
1230 _output_mapinfo = nullptr;
1231 if (status && CDSConfig::is_dumping_preimage_static_archive()) {
1232 tty->print_cr("%s AOTConfiguration recorded: %s",
1233 CDSConfig::has_temp_aot_config_file() ? "Temporary" : "", AOTConfiguration);
1234 if (CDSConfig::is_single_command_training()) {
1235 fork_and_dump_final_static_archive(CHECK);
1236 }
1237 }
1238
1239 if (!status) {
1240 THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1241 }
1242 }
1243
1244 bool AOTMetaspace::write_static_archive(ArchiveBuilder* builder,
1245 FileMapInfo* map_info,
1413 aot_log_warning(aot)("Preload Warning: Verification failed for %s because a %s was thrown: %s",
1414 ik->external_name(),
1415 current->pending_exception()->klass()->external_name(),
1416 message == nullptr ? "(no message)" : java_lang_String::as_utf8_string(message));
1417 CLEAR_PENDING_EXCEPTION;
1418 SystemDictionaryShared::set_class_has_failed_verification(ik);
1419 } else {
1420 assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1421 ik->compute_has_loops_flag_for_methods();
1422 }
1423 BytecodeVerificationLocal = saved;
1424 return true;
1425 } else {
1426 return false;
1427 }
1428 }
1429
1430 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1431 if (CDSConfig::is_dumping_heap()) {
1432 HeapShared::write_heap(&_mapped_heap_info, &_streamed_heap_info);
1433 } else if (!CDSConfig::is_dumping_preimage_static_archive()) {
1434 CDSConfig::log_reasons_for_not_dumping_heap();
1435 }
1436 }
1437
1438 void AOTMetaspace::set_aot_metaspace_range(void* base, void *static_top, void* top) {
1439 assert(base <= static_top && static_top <= top, "must be");
1440 _aot_metaspace_static_top = static_top;
1441 MetaspaceObj::set_aot_metaspace_range(base, top);
1442 }
1443
1444 bool AOTMetaspace::in_aot_cache_dynamic_region(void* p) {
1445 if ((p < MetaspaceObj::aot_metaspace_top()) &&
1446 (p >= _aot_metaspace_static_top)) {
1447 return true;
1448 } else {
1449 return false;
1450 }
1451 }
1452
1453 bool AOTMetaspace::in_aot_cache_static_region(void* p) {
1509 // This function is called when the JVM is unable to write the specified CDS archive due to an
1510 // unrecoverable error.
1511 void AOTMetaspace::unrecoverable_writing_error(const char* message) {
1512 writing_error(message);
1513 vm_direct_exit(1);
1514 }
1515
1516 // This function is called when the JVM is unable to write the specified CDS archive due to a
1517 // an error. The error will be propagated
1518 void AOTMetaspace::writing_error(const char* message) {
1519 aot_log_error(aot)("An error has occurred while writing the shared archive file.");
1520 if (message != nullptr) {
1521 aot_log_error(aot)("%s", message);
1522 }
1523 }
1524
1525 void AOTMetaspace::initialize_runtime_shared_and_meta_spaces() {
1526 assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1527 MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1528
1529 FileMapInfo* static_mapinfo = FileMapInfo::current_info();
1530 FileMapInfo* dynamic_mapinfo = nullptr;
1531
1532 if (static_mapinfo != nullptr) {
1533 aot_log_info(aot)("Core region alignment: %zu", static_mapinfo->core_region_alignment());
1534 dynamic_mapinfo = open_dynamic_archive();
1535
1536 aot_log_info(aot)("ArchiveRelocationMode: %d", ArchiveRelocationMode);
1537
1538 // First try to map at the requested address
1539 result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1540 if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1541 // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1542 // by the OS.
1543 aot_log_info(aot)("Try to map archive(s) at an alternative address");
1544 result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1545 }
1546 }
1547
1548 if (result == MAP_ARCHIVE_SUCCESS) {
1549 bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());
1572 } else {
1573 if (RequireSharedSpaces) {
1574 AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1575 } else {
1576 report_loading_error("Unable to map shared spaces");
1577 }
1578 }
1579 }
1580
1581 // If mapping failed and -XShare:on, the vm should exit
1582 bool has_failed = false;
1583 if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1584 has_failed = true;
1585 delete static_mapinfo;
1586 }
1587 if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1588 has_failed = true;
1589 delete dynamic_mapinfo;
1590 }
1591 if (RequireSharedSpaces && has_failed) {
1592 // static archive mapped but dynamic archive failed
1593 AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1594 }
1595 }
1596
1597 // This is called very early at VM start up to get the size of the cached_code region
1598 void AOTMetaspace::open_static_archive() {
1599 if (!UseSharedSpaces) { // FIXME -- is this still needed??
1600 return;
1601 }
1602 const char* static_archive = CDSConfig::input_static_archive_path();
1603 assert(static_archive != nullptr, "sanity");
1604 FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1605 if (!mapinfo->open_as_input()) {
1606 delete(mapinfo);
1607 log_info(cds)("Opening of static archive %s failed", static_archive);
1608 } else {
1609 FileMapRegion* r = mapinfo->region_at(AOTMetaspace::ac);
1610 AOTCacheAccess::set_aot_code_region_size(r->used_aligned());
1611 }
1612 }
1613
1614 FileMapInfo* AOTMetaspace::open_dynamic_archive() {
1615 if (CDSConfig::is_dumping_dynamic_archive()) {
1616 return nullptr;
1617 }
1618 const char* dynamic_archive = CDSConfig::input_dynamic_archive_path();
1619 if (dynamic_archive == nullptr) {
1620 return nullptr;
1621 }
1622
1623 FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1624 if (!mapinfo->open_as_input()) {
1625 delete(mapinfo);
1626 if (RequireSharedSpaces) {
1627 AOTMetaspace::unrecoverable_loading_error("Failed to initialize dynamic archive");
1628 }
1629 return nullptr;
1630 }
1631 return mapinfo;
2102 MemoryReserver::release(archive_space_rs);
2103 archive_space_rs = {};
2104 }
2105 if (class_space_rs.is_reserved()) {
2106 aot_log_debug(aot)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
2107 MemoryReserver::release(class_space_rs);
2108 class_space_rs = {};
2109 }
2110 }
2111 }
2112
2113 static int archive_regions[] = { AOTMetaspace::rw, AOTMetaspace::ro };
2114 static int archive_regions_count = 2;
2115
2116 MapArchiveResult AOTMetaspace::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
2117 assert(CDSConfig::is_using_archive(), "must be runtime");
2118 if (mapinfo == nullptr) {
2119 return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
2120 }
2121
2122 if (!mapinfo->validate_aot_class_linking()) {
2123 return MAP_ARCHIVE_OTHER_FAILURE;
2124 }
2125
2126 mapinfo->set_is_mapped(false);
2127 if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
2128 report_loading_error("Unable to map CDS archive -- core_region_alignment() expected: %zu"
2129 " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
2130 return MAP_ARCHIVE_OTHER_FAILURE;
2131 }
2132
2133 MapArchiveResult result =
2134 mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
2135
2136 if (result != MAP_ARCHIVE_SUCCESS) {
2137 unmap_archive(mapinfo);
2138 return result;
2139 }
2140
2141 if (!mapinfo->validate_class_location()) {
2142 unmap_archive(mapinfo);
2143 return MAP_ARCHIVE_OTHER_FAILURE;
2144 }
2145
|