5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "cds/aotArtifactFinder.hpp"
26 #include "cds/aotClassInitializer.hpp"
27 #include "cds/aotClassLinker.hpp"
28 #include "cds/aotClassLocation.hpp"
29 #include "cds/aotConstantPoolResolver.hpp"
30 #include "cds/aotLinkedClassBulkLoader.hpp"
31 #include "cds/aotLogging.hpp"
32 #include "cds/aotReferenceObjSupport.hpp"
33 #include "cds/archiveBuilder.hpp"
34 #include "cds/archiveHeapLoader.hpp"
35 #include "cds/archiveHeapWriter.hpp"
36 #include "cds/cds_globals.hpp"
37 #include "cds/cdsConfig.hpp"
38 #include "cds/cdsProtectionDomain.hpp"
39 #include "cds/classListParser.hpp"
40 #include "cds/classListWriter.hpp"
41 #include "cds/cppVtables.hpp"
42 #include "cds/dumpAllocStats.hpp"
43 #include "cds/dynamicArchive.hpp"
44 #include "cds/filemap.hpp"
45 #include "cds/finalImageRecipes.hpp"
46 #include "cds/heapShared.hpp"
47 #include "cds/lambdaFormInvokers.hpp"
48 #include "cds/lambdaProxyClassDictionary.hpp"
49 #include "cds/metaspaceShared.hpp"
50 #include "classfile/classLoaderDataGraph.hpp"
51 #include "classfile/classLoaderDataShared.hpp"
52 #include "classfile/classLoaderExt.hpp"
53 #include "classfile/javaClasses.inline.hpp"
54 #include "classfile/loaderConstraints.hpp"
55 #include "classfile/modules.hpp"
56 #include "classfile/placeholders.hpp"
57 #include "classfile/stringTable.hpp"
58 #include "classfile/symbolTable.hpp"
59 #include "classfile/systemDictionary.hpp"
60 #include "classfile/systemDictionaryShared.hpp"
61 #include "classfile/vmClasses.hpp"
62 #include "classfile/vmSymbols.hpp"
63 #include "code/aotCodeCache.hpp"
64 #include "code/codeCache.hpp"
65 #include "gc/shared/gcVMOperations.hpp"
66 #include "interpreter/bytecodeStream.hpp"
67 #include "interpreter/bytecodes.hpp"
68 #include "jvm_io.h"
69 #include "logging/log.hpp"
70 #include "logging/logMessage.hpp"
71 #include "logging/logStream.hpp"
72 #include "memory/memoryReserver.hpp"
73 #include "memory/metaspace.hpp"
74 #include "memory/metaspaceClosure.hpp"
75 #include "memory/oopFactory.hpp"
76 #include "memory/resourceArea.hpp"
77 #include "memory/universe.hpp"
78 #include "nmt/memTracker.hpp"
79 #include "oops/compressedKlass.hpp"
80 #include "oops/instanceMirrorKlass.hpp"
81 #include "oops/klass.inline.hpp"
82 #include "oops/objArrayOop.hpp"
83 #include "oops/oop.inline.hpp"
84 #include "oops/oopHandle.hpp"
85 #include "oops/trainingData.hpp"
86 #include "prims/jvmtiExport.hpp"
87 #include "runtime/arguments.hpp"
88 #include "runtime/globals.hpp"
89 #include "runtime/globals_extension.hpp"
90 #include "runtime/handles.inline.hpp"
91 #include "runtime/javaCalls.hpp"
92 #include "runtime/os.inline.hpp"
93 #include "runtime/safepointVerifiers.hpp"
94 #include "runtime/sharedRuntime.hpp"
95 #include "runtime/vmOperations.hpp"
96 #include "runtime/vmThread.hpp"
97 #include "sanitizers/leak.hpp"
98 #include "utilities/align.hpp"
99 #include "utilities/bitMap.inline.hpp"
100 #include "utilities/defaultStream.hpp"
101 #include "utilities/macros.hpp"
102 #include "utilities/ostream.hpp"
103 #include "utilities/resourceHash.hpp"
104
105 #include <sys/stat.h>
106
107 ReservedSpace MetaspaceShared::_symbol_rs;
108 VirtualSpace MetaspaceShared::_symbol_vs;
109 bool MetaspaceShared::_archive_loading_failed = false;
110 bool MetaspaceShared::_remapped_readwrite = false;
111 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
112 intx MetaspaceShared::_relocation_delta;
113 char* MetaspaceShared::_requested_base_address;
114 Array<Method*>* MetaspaceShared::_archived_method_handle_intrinsics = nullptr;
115 bool MetaspaceShared::_use_optimized_module_handling = true;
116
117 // The CDS archive is divided into the following regions:
118 // rw - read-write metadata
119 // ro - read-only metadata and read-only tables
120 // hp - heap region
121 // bm - bitmap for relocating the above 7 regions.
122 //
123 // The rw and ro regions are linearly allocated, in the order of rw->ro.
124 // These regions are aligned with MetaspaceShared::core_region_alignment().
125 //
126 // These 2 regions are populated in the following steps:
127 // [0] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
128 // temporarily allocated outside of the shared regions.
129 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
130 // [2] C++ vtables are copied into the rw region.
131 // [3] ArchiveBuilder copies RW metadata into the rw region.
132 // [4] ArchiveBuilder copies RO metadata into the ro region.
133 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
134 // are copied into the ro region as read-only tables.
135 //
276 }
277
278 // Arguments::default_SharedBaseAddress() is hard-coded in cds_globals.hpp. It must be carefully
279 // picked that (a) the align_up() below will always return a valid value; (b) none of
280 // the following asserts will fail.
281 aot_log_warning(aot)("SharedBaseAddress (" INTPTR_FORMAT ") is %s. Reverted to " INTPTR_FORMAT,
282 p2i((void*)SharedBaseAddress), err,
283 p2i((void*)Arguments::default_SharedBaseAddress()));
284
285 specified_base = (char*)Arguments::default_SharedBaseAddress();
286 aligned_base = align_up(specified_base, alignment);
287
288 // Make sure the default value of SharedBaseAddress specified in globals.hpp is sane.
289 assert(!shared_base_too_high(specified_base, aligned_base, cds_max), "Sanity");
290 assert(shared_base_valid(aligned_base), "Sanity");
291 return aligned_base;
292 }
293
294 void MetaspaceShared::initialize_for_static_dump() {
295 assert(CDSConfig::is_dumping_static_archive(), "sanity");
296 aot_log_info(aot)("Core region alignment: %zu", core_region_alignment());
297 // The max allowed size for CDS archive. We use this to limit SharedBaseAddress
298 // to avoid address space wrap around.
299 size_t cds_max;
300 const size_t reserve_alignment = core_region_alignment();
301
302 #ifdef _LP64
303 const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
304 cds_max = align_down(UnscaledClassSpaceMax, reserve_alignment);
305 #else
306 // We don't support archives larger than 256MB on 32-bit due to limited
307 // virtual address space.
308 cds_max = align_down(256*M, reserve_alignment);
309 #endif
310
311 _requested_base_address = compute_shared_base(cds_max);
312 SharedBaseAddress = (size_t)_requested_base_address;
313
314 size_t symbol_rs_size = LP64_ONLY(3 * G) NOT_LP64(128 * M);
315 _symbol_rs = MemoryReserver::reserve(symbol_rs_size,
720 _mirrors.at(i).release(Universe::vm_global());
721 }
722 }
723
724 void do_cld(ClassLoaderData* cld) {
725 assert(cld->is_alive(), "must be");
726 }
727
728 void do_klass(Klass* k) {
729 if (k->is_instance_klass()) {
730 _mirrors.append(OopHandle(Universe::vm_global(), k->java_mirror()));
731 }
732 }
733
734 const GrowableArray<OopHandle>* mirrors() const { return &_mirrors; }
735 };
736
737 // Check if we can eagerly link this class at dump time, so we can avoid the
738 // runtime linking overhead (especially verification)
739 bool MetaspaceShared::may_be_eagerly_linked(InstanceKlass* ik) {
740 if (!ik->can_be_verified_at_dumptime()) {
741 // For old classes, try to leave them in the unlinked state, so
742 // we can still store them in the archive. They must be
743 // linked/verified at runtime.
744 return false;
745 }
746 if (CDSConfig::is_dumping_dynamic_archive() && ik->defined_by_other_loaders()) {
747 // Linking of unregistered classes at this stage may cause more
748 // classes to be resolved, resulting in calls to ClassLoader.loadClass()
749 // that may not be expected by custom class loaders.
750 //
751 // It's OK to do this for the built-in loaders as we know they can
752 // tolerate this.
753 return false;
754 }
755 return true;
756 }
757
758 void MetaspaceShared::link_shared_classes(TRAPS) {
759 AOTClassLinker::initialize();
760 AOTClassInitializer::init_test_class(CHECK);
761
762 while (true) {
763 ResourceMark rm(THREAD);
764 CollectClassesForLinking collect_classes;
765 bool has_linked = false;
766 const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
767 for (int i = 0; i < mirrors->length(); i++) {
768 OopHandle mirror = mirrors->at(i);
769 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
770 if (may_be_eagerly_linked(ik)) {
771 has_linked |= try_link_class(THREAD, ik);
772 }
773 }
774
775 if (!has_linked) {
776 break;
777 }
778 // Class linking includes verification which may load more classes.
779 // Keep scanning until we have linked no more classes.
780 }
781
782 // Eargerly resolve all string constants in constant pools
783 {
784 ResourceMark rm(THREAD);
785 CollectClassesForLinking collect_classes;
786 const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
787 for (int i = 0; i < mirrors->length(); i++) {
788 OopHandle mirror = mirrors->at(i);
789 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
790 AOTConstantPoolResolver::preresolve_string_cp_entries(ik, CHECK);
791 }
792 }
793
794 if (CDSConfig::is_dumping_final_static_archive()) {
795 FinalImageRecipes::apply_recipes(CHECK);
796 }
797 }
798
799 // Preload classes from a list, populate the shared spaces and dump to a
800 // file.
801 void MetaspaceShared::preload_and_dump(TRAPS) {
802 CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
803 ResourceMark rm(THREAD);
804 HandleMark hm(THREAD);
805
806 if (CDSConfig::is_dumping_final_static_archive() && AOTPrintTrainingInfo) {
807 tty->print_cr("==================== archived_training_data ** before dumping ====================");
808 TrainingData::print_archived_training_data_on(tty);
809 }
810
811 StaticArchiveBuilder builder;
812 preload_and_dump_impl(builder, THREAD);
813 if (HAS_PENDING_EXCEPTION) {
814 if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
815 aot_log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
816 "%zuM", MaxHeapSize/M);
817 MetaspaceShared::writing_error();
818 } else {
819 oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
820 aot_log_error(aot)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
821 message == nullptr ? "(null)" : java_lang_String::as_utf8_string(message));
822 MetaspaceShared::writing_error(err_msg("Unexpected exception, use -Xlog:aot%s,exceptions=trace for detail",
823 CDSConfig::new_aot_flags_used() ? "" : ",cds"));
824 }
825 }
826
827 if (CDSConfig::new_aot_flags_used()) {
828 if (CDSConfig::is_dumping_preimage_static_archive()) {
829 // We are in the JVM that runs the training run. Continue execution,
830 // so that it can finish all clean-up and return the correct exit
831 // code to the OS.
832 } else {
833 // The JLI launcher only recognizes the "old" -Xshare:dump flag.
834 // When the new -XX:AOTMode=create flag is used, we can't return
835 // to the JLI launcher, as the launcher will fail when trying to
836 // run the main class, which is not what we want.
837 struct stat st;
838 if (os::stat(AOTCache, &st) != 0) {
839 tty->print_cr("AOTCache creation failed: %s", AOTCache);
840 vm_exit(0);
841 } else {
842 tty->print_cr("AOTCache creation is complete: %s " INT64_FORMAT " bytes", AOTCache, (int64_t)(st.st_size));
843 vm_exit(0);
844 }
845 }
846 }
847 }
848
849 #if INCLUDE_CDS_JAVA_HEAP && defined(_LP64)
850 void MetaspaceShared::adjust_heap_sizes_for_dumping() {
851 if (!CDSConfig::is_dumping_heap() || UseCompressedOops) {
852 return;
853 }
854 // CDS heap dumping requires all string oops to have an offset
855 // from the heap bottom that can be encoded in 32-bit.
856 julong max_heap_size = (julong)(4 * G);
857
858 if (MinHeapSize > max_heap_size) {
859 log_debug(aot)("Setting MinHeapSize to 4G for CDS dumping, original size = %zuM", MinHeapSize/M);
860 FLAG_SET_ERGO(MinHeapSize, max_heap_size);
861 }
862 if (InitialHeapSize > max_heap_size) {
863 log_debug(aot)("Setting InitialHeapSize to 4G for CDS dumping, original size = %zuM", InitialHeapSize/M);
864 FLAG_SET_ERGO(InitialHeapSize, max_heap_size);
903 }
904 }
905
906 // Some classes are used at CDS runtime but are not loaded, and therefore archived, at
907 // dumptime. We can perform dummmy calls to these classes at dumptime to ensure they
908 // are archived.
909 exercise_runtime_cds_code(CHECK);
910
911 aot_log_info(aot)("Loading classes to share: done.");
912 }
913
914 void MetaspaceShared::exercise_runtime_cds_code(TRAPS) {
915 // Exercise the manifest processing code
916 const char* dummy = "Manifest-Version: 1.0\n";
917 CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
918
919 // Exercise FileSystem and URL code
920 CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
921 }
922
923 void MetaspaceShared::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) {
924 if (CDSConfig::is_dumping_classic_static_archive()) {
925 // We are running with -Xshare:dump
926 preload_classes(CHECK);
927
928 if (SharedArchiveConfigFile) {
929 log_info(aot)("Reading extra data from %s ...", SharedArchiveConfigFile);
930 read_extra_data(THREAD, SharedArchiveConfigFile);
931 log_info(aot)("Reading extra data: done.");
932 }
933 }
934
935 if (CDSConfig::is_dumping_preimage_static_archive()) {
936 log_info(aot)("Reading lambda form invokers from JDK default classlist ...");
937 char default_classlist[JVM_MAXPATHLEN];
938 get_default_classlist(default_classlist, JVM_MAXPATHLEN);
939 struct stat statbuf;
940 if (os::stat(default_classlist, &statbuf) == 0) {
941 ClassListParser::parse_classlist(default_classlist,
942 ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
943 }
970 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
971 // fails verification, all other interfaces that were not specified in the classlist but
972 // are implemented by K are not verified.
973 link_shared_classes(CHECK);
974 log_info(aot)("Rewriting and linking classes: done");
975 TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
976
977 if (CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
978 LambdaFormInvokers::regenerate_holder_classes(CHECK);
979 }
980
981 #if INCLUDE_CDS_JAVA_HEAP
982 if (CDSConfig::is_dumping_heap()) {
983 ArchiveHeapWriter::init();
984
985 if (CDSConfig::is_dumping_full_module_graph()) {
986 ClassLoaderDataShared::ensure_module_entry_tables_exist();
987 HeapShared::reset_archived_object_states(CHECK);
988 }
989
990 AOTReferenceObjSupport::initialize(CHECK);
991 AOTReferenceObjSupport::stabilize_cached_reference_objects(CHECK);
992
993 if (CDSConfig::is_initing_classes_at_dump_time()) {
994 // java.lang.Class::reflectionFactory cannot be archived yet. We set this field
995 // to null, and it will be initialized again at runtime.
996 log_debug(aot)("Resetting Class::reflectionFactory");
997 TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
998 Symbol* method_sig = vmSymbols::void_method_signature();
999 JavaValue result(T_VOID);
1000 JavaCalls::call_static(&result, vmClasses::Class_klass(),
1001 method_name, method_sig, CHECK);
1002
1003 // Perhaps there is a way to avoid hard-coding these names here.
1004 // See discussion in JDK-8342481.
1005 }
1006
1007 // Do this at the very end, when no Java code will be executed. Otherwise
1008 // some new strings may be added to the intern table.
1009 StringTable::allocate_shared_strings_array(CHECK);
1010 } else {
1011 log_info(aot)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling");
1012 CDSConfig::stop_using_optimized_module_handling();
1013 }
1014 #endif
1015
1016 VM_PopulateDumpSharedSpace op(builder);
1017 VMThread::execute(&op);
1018
1019 if (AOTCodeCache::is_on_for_dump() && CDSConfig::is_dumping_final_static_archive()) {
1020 CDSConfig::enable_dumping_aot_code();
1021 {
1022 builder.start_ac_region();
1023 // Write the contents to AOT code region and close AOTCodeCache before packing the region
1024 AOTCodeCache::close();
1025 builder.end_ac_region();
1026 }
1027 CDSConfig::disable_dumping_aot_code();
1028 }
1029
1030 bool status = write_static_archive(&builder, op.map_info(), op.heap_info());
1031 if (status && CDSConfig::is_dumping_preimage_static_archive()) {
1032 tty->print_cr("%s AOTConfiguration recorded: %s",
1033 CDSConfig::has_temp_aot_config_file() ? "Temporary" : "", AOTConfiguration);
1034 if (CDSConfig::is_single_command_training()) {
1035 fork_and_dump_final_static_archive(CHECK);
1036 }
1037 }
1038
1039 if (!status) {
1040 THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1041 }
1042 }
1043
1044 bool MetaspaceShared::write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info) {
1045 // relocate the data so that it can be mapped to MetaspaceShared::requested_base_address()
1046 // without runtime relocation.
1047 builder->relocate_to_requested();
1048
1049 map_info->open_as_output();
1050 if (!map_info->is_open()) {
1213 if (HAS_PENDING_EXCEPTION) {
1214 ResourceMark rm(THREAD);
1215 aot_log_warning(aot)("Preload Warning: Verification failed for %s",
1216 ik->external_name());
1217 CLEAR_PENDING_EXCEPTION;
1218 SystemDictionaryShared::set_class_has_failed_verification(ik);
1219 } else {
1220 assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1221 ik->compute_has_loops_flag_for_methods();
1222 }
1223 BytecodeVerificationLocal = saved;
1224 return true;
1225 } else {
1226 return false;
1227 }
1228 }
1229
1230 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1231 if (CDSConfig::is_dumping_heap()) {
1232 HeapShared::write_heap(&_heap_info);
1233 } else {
1234 CDSConfig::log_reasons_for_not_dumping_heap();
1235 }
1236 }
1237
1238 void MetaspaceShared::set_shared_metaspace_range(void* base, void *static_top, void* top) {
1239 assert(base <= static_top && static_top <= top, "must be");
1240 _shared_metaspace_static_top = static_top;
1241 MetaspaceObj::set_shared_metaspace_range(base, top);
1242 }
1243
1244 bool MetaspaceShared::is_shared_dynamic(void* p) {
1245 if ((p < MetaspaceObj::shared_metaspace_top()) &&
1246 (p >= _shared_metaspace_static_top)) {
1247 return true;
1248 } else {
1249 return false;
1250 }
1251 }
1252
1253 bool MetaspaceShared::is_shared_static(void* p) {
1271 } else if (CDSConfig::new_aot_flags_used()) {
1272 vm_exit_during_initialization("Unable to use AOT cache.", nullptr);
1273 } else {
1274 vm_exit_during_initialization("Unable to use shared archive.", nullptr);
1275 }
1276 }
1277
1278 void MetaspaceShared::report_loading_error(const char* format, ...) {
1279 // When using AOT cache, errors messages are always printed on the error channel.
1280 LogStream ls_aot(LogLevel::Error, LogTagSetMapping<LOG_TAGS(aot)>::tagset());
1281
1282 // If we are loading load the default CDS archive, it may fail due to incompatible VM options.
1283 // Print at the info level to avoid excessive verbosity.
1284 // However, if the user has specified a CDS archive (or AOT cache), they would be interested in
1285 // knowing that the loading fails, so we print at the error level.
1286 LogLevelType level = (!CDSConfig::is_using_archive() || CDSConfig::is_using_only_default_archive()) ?
1287 LogLevel::Info : LogLevel::Error;
1288 LogStream ls_cds(level, LogTagSetMapping<LOG_TAGS(cds)>::tagset());
1289
1290 LogStream& ls = CDSConfig::new_aot_flags_used() ? ls_aot : ls_cds;
1291 va_list ap;
1292 va_start(ap, format);
1293
1294 static bool printed_error = false;
1295 if (!printed_error) { // No need for locks. Loading error checks happen only in main thread.
1296 ls.print_cr("An error has occurred while processing the %s. Run with -Xlog:%s for details.",
1297 CDSConfig::type_of_archive_being_loaded(), CDSConfig::new_aot_flags_used() ? "aot" : "aot,cds");
1298 printed_error = true;
1299 }
1300 ls.vprint_cr(format, ap);
1301
1302 va_end(ap);
1303 }
1304
1305 // This function is called when the JVM is unable to write the specified CDS archive due to an
1306 // unrecoverable error.
1307 void MetaspaceShared::unrecoverable_writing_error(const char* message) {
1308 writing_error(message);
1309 vm_direct_exit(1);
1310 }
1311
1312 // This function is called when the JVM is unable to write the specified CDS archive due to a
1313 // an error. The error will be propagated
1314 void MetaspaceShared::writing_error(const char* message) {
1315 aot_log_error(aot)("An error has occurred while writing the shared archive file.");
1316 if (message != nullptr) {
1317 aot_log_error(aot)("%s", message);
1318 }
1319 }
1320
1321 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
1322 assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1323 MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1324
1325 FileMapInfo* static_mapinfo = open_static_archive();
1326 FileMapInfo* dynamic_mapinfo = nullptr;
1327
1328 if (static_mapinfo != nullptr) {
1329 aot_log_info(aot)("Core region alignment: %zu", static_mapinfo->core_region_alignment());
1330 dynamic_mapinfo = open_dynamic_archive();
1331
1332 aot_log_info(aot)("ArchiveRelocationMode: %d", ArchiveRelocationMode);
1333
1334 // First try to map at the requested address
1335 result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1336 if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1337 // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1338 // by the OS.
1339 aot_log_info(aot)("Try to map archive(s) at an alternative address");
1340 result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1341 }
1342 }
1343
1344 if (result == MAP_ARCHIVE_SUCCESS) {
1345 bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());
1368 } else {
1369 if (RequireSharedSpaces) {
1370 MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1371 } else {
1372 report_loading_error("Unable to map shared spaces");
1373 }
1374 }
1375 }
1376
1377 // If mapping failed and -XShare:on, the vm should exit
1378 bool has_failed = false;
1379 if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1380 has_failed = true;
1381 delete static_mapinfo;
1382 }
1383 if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1384 has_failed = true;
1385 delete dynamic_mapinfo;
1386 }
1387 if (RequireSharedSpaces && has_failed) {
1388 MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1389 }
1390 }
1391
1392 FileMapInfo* MetaspaceShared::open_static_archive() {
1393 const char* static_archive = CDSConfig::input_static_archive_path();
1394 assert(static_archive != nullptr, "sanity");
1395 FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1396 if (!mapinfo->open_as_input()) {
1397 delete(mapinfo);
1398 return nullptr;
1399 }
1400 return mapinfo;
1401 }
1402
1403 FileMapInfo* MetaspaceShared::open_dynamic_archive() {
1404 if (CDSConfig::is_dumping_dynamic_archive()) {
1405 return nullptr;
1406 }
1407 const char* dynamic_archive = CDSConfig::input_dynamic_archive_path();
1408 if (dynamic_archive == nullptr) {
1409 return nullptr;
1410 }
1411
1412 FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1413 if (!mapinfo->open_as_input()) {
1414 delete(mapinfo);
1415 if (RequireSharedSpaces) {
1416 MetaspaceShared::unrecoverable_loading_error("Failed to initialize dynamic archive");
1417 }
1418 return nullptr;
1419 }
1420 return mapinfo;
1866 MemoryReserver::release(archive_space_rs);
1867 archive_space_rs = {};
1868 }
1869 if (class_space_rs.is_reserved()) {
1870 aot_log_debug(aot)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
1871 MemoryReserver::release(class_space_rs);
1872 class_space_rs = {};
1873 }
1874 }
1875 }
1876
1877 static int archive_regions[] = { MetaspaceShared::rw, MetaspaceShared::ro };
1878 static int archive_regions_count = 2;
1879
1880 MapArchiveResult MetaspaceShared::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
1881 assert(CDSConfig::is_using_archive(), "must be runtime");
1882 if (mapinfo == nullptr) {
1883 return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
1884 }
1885
1886 mapinfo->set_is_mapped(false);
1887 if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
1888 report_loading_error("Unable to map CDS archive -- core_region_alignment() expected: %zu"
1889 " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
1890 return MAP_ARCHIVE_OTHER_FAILURE;
1891 }
1892
1893 MapArchiveResult result =
1894 mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
1895
1896 if (result != MAP_ARCHIVE_SUCCESS) {
1897 unmap_archive(mapinfo);
1898 return result;
1899 }
1900
1901 if (!mapinfo->validate_class_location()) {
1902 unmap_archive(mapinfo);
1903 return MAP_ARCHIVE_OTHER_FAILURE;
1904 }
1905
1997
1998 if (PrintSharedArchiveAndExit) {
1999 // Print archive names
2000 if (dynamic_mapinfo != nullptr) {
2001 tty->print_cr("\n\nBase archive name: %s", CDSConfig::input_static_archive_path());
2002 tty->print_cr("Base archive version %d", static_mapinfo->version());
2003 } else {
2004 tty->print_cr("Static archive name: %s", static_mapinfo->full_path());
2005 tty->print_cr("Static archive version %d", static_mapinfo->version());
2006 }
2007
2008 SystemDictionaryShared::print_shared_archive(tty);
2009 if (dynamic_mapinfo != nullptr) {
2010 tty->print_cr("\n\nDynamic archive name: %s", dynamic_mapinfo->full_path());
2011 tty->print_cr("Dynamic archive version %d", dynamic_mapinfo->version());
2012 SystemDictionaryShared::print_shared_archive(tty, false/*dynamic*/);
2013 }
2014
2015 TrainingData::print_archived_training_data_on(tty);
2016
2017 if (AOTCodeCache::is_on_for_use()) {
2018 tty->print_cr("\n\nAOT Code");
2019 AOTCodeCache::print_on(tty);
2020 }
2021
2022 // collect shared symbols and strings
2023 CountSharedSymbols cl;
2024 SymbolTable::shared_symbols_do(&cl);
2025 tty->print_cr("Number of shared symbols: %d", cl.total());
2026 tty->print_cr("Number of shared strings: %zu", StringTable::shared_entry_count());
2027 tty->print_cr("VM version: %s\r\n", static_mapinfo->vm_version());
2028 if (FileMapInfo::current_info() == nullptr || _archive_loading_failed) {
2029 tty->print_cr("archive is invalid");
2030 vm_exit(1);
2031 } else {
2032 tty->print_cr("archive is valid");
2033 vm_exit(0);
2034 }
2035 }
2036 }
2037
2038 // JVM/TI RedefineClasses() support:
2039 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
2040 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
|
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "cds/aotCacheAccess.hpp"
26 #include "cds/aotClassInitializer.hpp"
27 #include "cds/aotArtifactFinder.hpp"
28 #include "cds/aotClassInitializer.hpp"
29 #include "cds/aotClassLinker.hpp"
30 #include "cds/aotClassLocation.hpp"
31 #include "cds/aotConstantPoolResolver.hpp"
32 #include "cds/aotLinkedClassBulkLoader.hpp"
33 #include "cds/aotLogging.hpp"
34 #include "cds/aotReferenceObjSupport.hpp"
35 #include "cds/archiveBuilder.hpp"
36 #include "cds/archiveHeapLoader.hpp"
37 #include "cds/archiveHeapWriter.hpp"
38 #include "cds/cds_globals.hpp"
39 #include "cds/cdsConfig.hpp"
40 #include "cds/cdsProtectionDomain.hpp"
41 #include "cds/classListParser.hpp"
42 #include "cds/classListWriter.hpp"
43 #include "cds/cppVtables.hpp"
44 #include "cds/dumpAllocStats.hpp"
45 #include "cds/dynamicArchive.hpp"
46 #include "cds/filemap.hpp"
47 #include "cds/finalImageRecipes.hpp"
48 #include "cds/heapShared.hpp"
49 #include "cds/lambdaFormInvokers.hpp"
50 #include "cds/lambdaProxyClassDictionary.hpp"
51 #include "cds/metaspaceShared.hpp"
52 #include "classfile/classLoaderDataGraph.hpp"
53 #include "classfile/classLoaderDataShared.hpp"
54 #include "classfile/classLoaderExt.hpp"
55 #include "classfile/javaClasses.inline.hpp"
56 #include "classfile/loaderConstraints.hpp"
57 #include "classfile/modules.hpp"
58 #include "classfile/placeholders.hpp"
59 #include "classfile/stringTable.hpp"
60 #include "classfile/symbolTable.hpp"
61 #include "classfile/systemDictionary.hpp"
62 #include "classfile/systemDictionaryShared.hpp"
63 #include "classfile/vmClasses.hpp"
64 #include "classfile/vmSymbols.hpp"
65 #include "code/aotCodeCache.hpp"
66 #include "code/codeCache.hpp"
67 #include "compiler/compileBroker.hpp"
68 #include "compiler/precompiler.hpp"
69 #include "gc/shared/gcVMOperations.hpp"
70 #include "interpreter/bytecodeStream.hpp"
71 #include "interpreter/bytecodes.hpp"
72 #include "jvm_io.h"
73 #include "logging/log.hpp"
74 #include "logging/logMessage.hpp"
75 #include "logging/logStream.hpp"
76 #include "memory/memoryReserver.hpp"
77 #include "memory/metaspace.hpp"
78 #include "memory/metaspaceClosure.hpp"
79 #include "memory/oopFactory.hpp"
80 #include "memory/resourceArea.hpp"
81 #include "memory/universe.hpp"
82 #include "nmt/memTracker.hpp"
83 #include "oops/compressedKlass.hpp"
84 #include "oops/instanceMirrorKlass.hpp"
85 #include "oops/klass.inline.hpp"
86 #include "oops/method.inline.hpp"
87 #include "oops/objArrayOop.hpp"
88 #include "oops/oop.inline.hpp"
89 #include "oops/oopHandle.hpp"
90 #include "oops/trainingData.hpp"
91 #include "prims/jvmtiExport.hpp"
92 #include "prims/whitebox.hpp"
93 #include "runtime/arguments.hpp"
94 #include "runtime/globals.hpp"
95 #include "runtime/globals_extension.hpp"
96 #include "runtime/handles.inline.hpp"
97 #include "runtime/javaCalls.hpp"
98 #include "runtime/os.inline.hpp"
99 #include "runtime/safepointVerifiers.hpp"
100 #include "runtime/sharedRuntime.hpp"
101 #include "runtime/vmOperations.hpp"
102 #include "runtime/vmThread.hpp"
103 #include "sanitizers/leak.hpp"
104 #include "services/management.hpp"
105 #include "utilities/align.hpp"
106 #include "utilities/bitMap.inline.hpp"
107 #include "utilities/defaultStream.hpp"
108 #include "utilities/macros.hpp"
109 #include "utilities/ostream.hpp"
110 #include "utilities/resourceHash.hpp"
111
112 #include <sys/stat.h>
113
114 ReservedSpace MetaspaceShared::_symbol_rs;
115 VirtualSpace MetaspaceShared::_symbol_vs;
116 bool MetaspaceShared::_archive_loading_failed = false;
117 bool MetaspaceShared::_remapped_readwrite = false;
118 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
119 intx MetaspaceShared::_relocation_delta;
120 char* MetaspaceShared::_requested_base_address;
121 Array<Method*>* MetaspaceShared::_archived_method_handle_intrinsics = nullptr;
122 bool MetaspaceShared::_use_optimized_module_handling = true;
123 int volatile MetaspaceShared::_preimage_static_archive_dumped = 0;
124 jlong MetaspaceShared::_preimage_static_archive_recording_duration = 0;
125
126 // The CDS archive is divided into the following regions:
127 // rw - read-write metadata
128 // ro - read-only metadata and read-only tables
129 // hp - heap region
130 // bm - bitmap for relocating the above 7 regions.
131 //
132 // The rw and ro regions are linearly allocated, in the order of rw->ro.
133 // These regions are aligned with MetaspaceShared::core_region_alignment().
134 //
135 // These 2 regions are populated in the following steps:
136 // [0] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
137 // temporarily allocated outside of the shared regions.
138 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
139 // [2] C++ vtables are copied into the rw region.
140 // [3] ArchiveBuilder copies RW metadata into the rw region.
141 // [4] ArchiveBuilder copies RO metadata into the ro region.
142 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
143 // are copied into the ro region as read-only tables.
144 //
285 }
286
287 // Arguments::default_SharedBaseAddress() is hard-coded in cds_globals.hpp. It must be carefully
288 // picked that (a) the align_up() below will always return a valid value; (b) none of
289 // the following asserts will fail.
290 aot_log_warning(aot)("SharedBaseAddress (" INTPTR_FORMAT ") is %s. Reverted to " INTPTR_FORMAT,
291 p2i((void*)SharedBaseAddress), err,
292 p2i((void*)Arguments::default_SharedBaseAddress()));
293
294 specified_base = (char*)Arguments::default_SharedBaseAddress();
295 aligned_base = align_up(specified_base, alignment);
296
297 // Make sure the default value of SharedBaseAddress specified in globals.hpp is sane.
298 assert(!shared_base_too_high(specified_base, aligned_base, cds_max), "Sanity");
299 assert(shared_base_valid(aligned_base), "Sanity");
300 return aligned_base;
301 }
302
303 void MetaspaceShared::initialize_for_static_dump() {
304 assert(CDSConfig::is_dumping_static_archive(), "sanity");
305
306 if (CDSConfig::is_dumping_preimage_static_archive() || CDSConfig::is_dumping_final_static_archive()) {
307 if (!((UseG1GC || UseParallelGC || UseSerialGC || UseEpsilonGC || UseShenandoahGC) && UseCompressedClassPointers)) {
308 const char* error;
309 if (CDSConfig::is_dumping_preimage_static_archive()) {
310 error = "Cannot create the AOT configuration file";
311 } else {
312 error = "Cannot create the AOT cache";
313 }
314
315 vm_exit_during_initialization(error,
316 "UseCompressedClassPointers must be enabled, and collector must be G1, Parallel, Serial, Epsilon, or Shenandoah");
317 }
318 }
319
320 aot_log_info(aot)("Core region alignment: %zu", core_region_alignment());
321 // The max allowed size for CDS archive. We use this to limit SharedBaseAddress
322 // to avoid address space wrap around.
323 size_t cds_max;
324 const size_t reserve_alignment = core_region_alignment();
325
326 #ifdef _LP64
327 const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
328 cds_max = align_down(UnscaledClassSpaceMax, reserve_alignment);
329 #else
330 // We don't support archives larger than 256MB on 32-bit due to limited
331 // virtual address space.
332 cds_max = align_down(256*M, reserve_alignment);
333 #endif
334
335 _requested_base_address = compute_shared_base(cds_max);
336 SharedBaseAddress = (size_t)_requested_base_address;
337
338 size_t symbol_rs_size = LP64_ONLY(3 * G) NOT_LP64(128 * M);
339 _symbol_rs = MemoryReserver::reserve(symbol_rs_size,
744 _mirrors.at(i).release(Universe::vm_global());
745 }
746 }
747
748 void do_cld(ClassLoaderData* cld) {
749 assert(cld->is_alive(), "must be");
750 }
751
752 void do_klass(Klass* k) {
753 if (k->is_instance_klass()) {
754 _mirrors.append(OopHandle(Universe::vm_global(), k->java_mirror()));
755 }
756 }
757
758 const GrowableArray<OopHandle>* mirrors() const { return &_mirrors; }
759 };
760
761 // Check if we can eagerly link this class at dump time, so we can avoid the
762 // runtime linking overhead (especially verification)
763 bool MetaspaceShared::may_be_eagerly_linked(InstanceKlass* ik) {
764 if (CDSConfig::preserve_all_dumptime_verification_states(ik)) {
765 assert(ik->can_be_verified_at_dumptime(), "sanity");
766 }
767 if (!ik->can_be_verified_at_dumptime()) {
768 // For old classes, try to leave them in the unlinked state, so
769 // we can still store them in the archive. They must be
770 // linked/verified at runtime.
771 return false;
772 }
773 if (CDSConfig::is_dumping_dynamic_archive() && ik->defined_by_other_loaders()) {
774 // Linking of unregistered classes at this stage may cause more
775 // classes to be resolved, resulting in calls to ClassLoader.loadClass()
776 // that may not be expected by custom class loaders.
777 //
778 // It's OK to do this for the built-in loaders as we know they can
779 // tolerate this.
780 return false;
781 }
782 return true;
783 }
784
785 void MetaspaceShared::link_shared_classes(TRAPS) {
786 AOTClassLinker::initialize();
787 AOTClassInitializer::init_test_class(CHECK);
788
789 if (CDSConfig::is_dumping_final_static_archive()) {
790 FinalImageRecipes::apply_recipes(CHECK);
791 }
792
793 while (true) {
794 ResourceMark rm(THREAD);
795 CollectClassesForLinking collect_classes;
796 bool has_linked = false;
797 const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
798 for (int i = 0; i < mirrors->length(); i++) {
799 OopHandle mirror = mirrors->at(i);
800 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
801 if (may_be_eagerly_linked(ik)) {
802 has_linked |= try_link_class(THREAD, ik);
803 }
804 if (CDSConfig::is_dumping_heap() && ik->is_linked() && !ik->is_initialized()) {
805 AOTClassInitializer::maybe_preinit_class(ik, CHECK);
806 }
807 }
808
809 if (!has_linked) {
810 break;
811 }
812 // Class linking includes verification which may load more classes.
813 // Keep scanning until we have linked no more classes.
814 }
815
816 // Eargerly resolve all string constants in constant pools
817 {
818 ResourceMark rm(THREAD);
819 CollectClassesForLinking collect_classes;
820 const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
821 for (int i = 0; i < mirrors->length(); i++) {
822 OopHandle mirror = mirrors->at(i);
823 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
824 AOTConstantPoolResolver::preresolve_string_cp_entries(ik, CHECK);
825 if (CDSConfig::is_dumping_preimage_static_archive()) {
826 FinalImageRecipes::add_reflection_data_flags(ik, CHECK);
827 }
828 }
829 }
830 }
831
832 // Preload classes from a list, populate the shared spaces and dump to a
833 // file.
834 void MetaspaceShared::preload_and_dump(TRAPS) {
835 CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
836 ResourceMark rm(THREAD);
837 HandleMark hm(THREAD);
838
839 if (CDSConfig::is_dumping_final_static_archive() && AOTPrintTrainingInfo) {
840 tty->print_cr("==================== archived_training_data ** before dumping ====================");
841 TrainingData::print_archived_training_data_on(tty);
842 }
843
844 StaticArchiveBuilder builder;
845 preload_and_dump_impl(builder, THREAD);
846 if (HAS_PENDING_EXCEPTION) {
847 if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
848 aot_log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
849 "%zuM", MaxHeapSize/M);
850 MetaspaceShared::writing_error();
851 } else {
852 oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
853 aot_log_error(aot)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
854 message == nullptr ? "(null)" : java_lang_String::as_utf8_string(message));
855 MetaspaceShared::writing_error(err_msg("Unexpected exception, use -Xlog:aot%s,exceptions=trace for detail",
856 CDSConfig::new_aot_flags_used() ? "" : ",cds"));
857 }
858 }
859
860 if (CDSConfig::new_aot_flags_used()) {
861 if (CDSConfig::is_dumping_preimage_static_archive()) {
862 // We are in the JVM that runs the training run. Continue execution,
863 // so that it can finish all clean-up and return the correct exit
864 // code to the OS.
865 } else {
866 // The JLI launcher only recognizes the "old" -Xshare:dump flag.
867 // When the new -XX:AOTMode=create flag is used, we can't return
868 // to the JLI launcher, as the launcher will fail when trying to
869 // run the main class, which is not what we want.
870 struct stat st;
871 if (os::stat(AOTCache, &st) != 0) {
872 tty->print_cr("AOTCache creation failed: %s", AOTCache);
873 } else {
874 tty->print_cr("AOTCache creation is complete: %s " INT64_FORMAT " bytes", AOTCache, (int64_t)(st.st_size));
875 }
876 vm_direct_exit(0);
877 }
878 }
879 }
880
881 #if INCLUDE_CDS_JAVA_HEAP && defined(_LP64)
882 void MetaspaceShared::adjust_heap_sizes_for_dumping() {
883 if (!CDSConfig::is_dumping_heap() || UseCompressedOops) {
884 return;
885 }
886 // CDS heap dumping requires all string oops to have an offset
887 // from the heap bottom that can be encoded in 32-bit.
888 julong max_heap_size = (julong)(4 * G);
889
890 if (MinHeapSize > max_heap_size) {
891 log_debug(aot)("Setting MinHeapSize to 4G for CDS dumping, original size = %zuM", MinHeapSize/M);
892 FLAG_SET_ERGO(MinHeapSize, max_heap_size);
893 }
894 if (InitialHeapSize > max_heap_size) {
895 log_debug(aot)("Setting InitialHeapSize to 4G for CDS dumping, original size = %zuM", InitialHeapSize/M);
896 FLAG_SET_ERGO(InitialHeapSize, max_heap_size);
935 }
936 }
937
938 // Some classes are used at CDS runtime but are not loaded, and therefore archived, at
939 // dumptime. We can perform dummmy calls to these classes at dumptime to ensure they
940 // are archived.
941 exercise_runtime_cds_code(CHECK);
942
943 aot_log_info(aot)("Loading classes to share: done.");
944 }
945
946 void MetaspaceShared::exercise_runtime_cds_code(TRAPS) {
947 // Exercise the manifest processing code
948 const char* dummy = "Manifest-Version: 1.0\n";
949 CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
950
951 // Exercise FileSystem and URL code
952 CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
953 }
954
955 bool MetaspaceShared::is_recording_preimage_static_archive() {
956 if (CDSConfig::is_dumping_preimage_static_archive()) {
957 return _preimage_static_archive_dumped == 0;
958 }
959 return false;
960 }
961
962 jlong MetaspaceShared::get_preimage_static_archive_recording_duration() {
963 if (CDSConfig::is_dumping_preimage_static_archive()) {
964 if (_preimage_static_archive_recording_duration == 0) {
965 // The recording has not yet finished so return the current elapsed time.
966 return Management::ticks_to_ms(os::elapsed_counter());
967 }
968 return _preimage_static_archive_recording_duration;
969 }
970 return 0;
971 }
972
973 void MetaspaceShared::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) {
974 if (CDSConfig::is_dumping_preimage_static_archive()) {
975 if (Atomic::cmpxchg(&_preimage_static_archive_dumped, 0, 1) != 0) {
976 return;
977 }
978 _preimage_static_archive_recording_duration = Management::ticks_to_ms(os::elapsed_counter());
979 }
980
981 if (CDSConfig::is_dumping_classic_static_archive()) {
982 // We are running with -Xshare:dump
983 preload_classes(CHECK);
984
985 if (SharedArchiveConfigFile) {
986 log_info(aot)("Reading extra data from %s ...", SharedArchiveConfigFile);
987 read_extra_data(THREAD, SharedArchiveConfigFile);
988 log_info(aot)("Reading extra data: done.");
989 }
990 }
991
992 if (CDSConfig::is_dumping_preimage_static_archive()) {
993 log_info(aot)("Reading lambda form invokers from JDK default classlist ...");
994 char default_classlist[JVM_MAXPATHLEN];
995 get_default_classlist(default_classlist, JVM_MAXPATHLEN);
996 struct stat statbuf;
997 if (os::stat(default_classlist, &statbuf) == 0) {
998 ClassListParser::parse_classlist(default_classlist,
999 ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
1000 }
1027 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1028 // fails verification, all other interfaces that were not specified in the classlist but
1029 // are implemented by K are not verified.
1030 link_shared_classes(CHECK);
1031 log_info(aot)("Rewriting and linking classes: done");
1032 TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
1033
1034 if (CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
1035 LambdaFormInvokers::regenerate_holder_classes(CHECK);
1036 }
1037
1038 #if INCLUDE_CDS_JAVA_HEAP
1039 if (CDSConfig::is_dumping_heap()) {
1040 ArchiveHeapWriter::init();
1041
1042 if (CDSConfig::is_dumping_full_module_graph()) {
1043 ClassLoaderDataShared::ensure_module_entry_tables_exist();
1044 HeapShared::reset_archived_object_states(CHECK);
1045 }
1046
1047 if (ArchiveLoaderLookupCache) {
1048 SystemDictionaryShared::create_loader_positive_lookup_cache(CHECK);
1049 }
1050
1051 AOTReferenceObjSupport::initialize(CHECK);
1052 AOTReferenceObjSupport::stabilize_cached_reference_objects(CHECK);
1053
1054 if (CDSConfig::is_initing_classes_at_dump_time()) {
1055 // java.lang.Class::reflectionFactory cannot be archived yet. We set this field
1056 // to null, and it will be initialized again at runtime.
1057 log_debug(aot)("Resetting Class::reflectionFactory");
1058 TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
1059 Symbol* method_sig = vmSymbols::void_method_signature();
1060 JavaValue result(T_VOID);
1061 JavaCalls::call_static(&result, vmClasses::Class_klass(),
1062 method_name, method_sig, CHECK);
1063
1064 // Perhaps there is a way to avoid hard-coding these names here.
1065 // See discussion in JDK-8342481.
1066 }
1067
1068 // Do this at the very end, when no Java code will be executed. Otherwise
1069 // some new strings may be added to the intern table.
1070 StringTable::allocate_shared_strings_array(CHECK);
1071 } else {
1072 log_info(aot)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling");
1073 CDSConfig::stop_using_optimized_module_handling();
1074 }
1075 #endif
1076
1077 VM_PopulateDumpSharedSpace op(builder);
1078 VMThread::execute(&op);
1079 FileMapInfo* mapinfo = op.map_info();
1080 ArchiveHeapInfo* heap_info = op.heap_info();
1081
1082 if (CDSConfig::is_dumping_final_static_archive()) {
1083 if (AOTCodeCache::is_caching_enabled()) {
1084 if (log_is_enabled(Info, cds, jit)) {
1085 AOTCacheAccess::test_heap_access_api();
1086 }
1087
1088 // We have just created the final image. Let's run the AOT compiler
1089 if (AOTPrintTrainingInfo) {
1090 tty->print_cr("==================== archived_training_data ** after dumping ====================");
1091 TrainingData::print_archived_training_data_on(tty);
1092 }
1093
1094 CDSConfig::enable_dumping_aot_code();
1095 {
1096 builder.start_ac_region();
1097 if (AOTCodeCache::is_dumping_code()) {
1098 Precompiler::compile_cached_code(&builder, CHECK);
1099 }
1100 // Write the contents to aot code region and close AOTCodeCache before packing the region
1101 AOTCodeCache::close();
1102 builder.end_ac_region();
1103 }
1104 CDSConfig::disable_dumping_aot_code();
1105 }
1106 }
1107
1108 bool status = write_static_archive(&builder, mapinfo, heap_info);
1109 if (status && CDSConfig::is_dumping_preimage_static_archive()) {
1110 tty->print_cr("%s AOTConfiguration recorded: %s",
1111 CDSConfig::has_temp_aot_config_file() ? "Temporary" : "", AOTConfiguration);
1112 if (CDSConfig::is_single_command_training()) {
1113 fork_and_dump_final_static_archive(CHECK);
1114 }
1115 }
1116
1117 if (!status) {
1118 THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1119 }
1120 }
1121
1122 bool MetaspaceShared::write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info) {
1123 // relocate the data so that it can be mapped to MetaspaceShared::requested_base_address()
1124 // without runtime relocation.
1125 builder->relocate_to_requested();
1126
1127 map_info->open_as_output();
1128 if (!map_info->is_open()) {
1291 if (HAS_PENDING_EXCEPTION) {
1292 ResourceMark rm(THREAD);
1293 aot_log_warning(aot)("Preload Warning: Verification failed for %s",
1294 ik->external_name());
1295 CLEAR_PENDING_EXCEPTION;
1296 SystemDictionaryShared::set_class_has_failed_verification(ik);
1297 } else {
1298 assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1299 ik->compute_has_loops_flag_for_methods();
1300 }
1301 BytecodeVerificationLocal = saved;
1302 return true;
1303 } else {
1304 return false;
1305 }
1306 }
1307
1308 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1309 if (CDSConfig::is_dumping_heap()) {
1310 HeapShared::write_heap(&_heap_info);
1311 } else if (!CDSConfig::is_dumping_preimage_static_archive()) {
1312 CDSConfig::log_reasons_for_not_dumping_heap();
1313 }
1314 }
1315
1316 void MetaspaceShared::set_shared_metaspace_range(void* base, void *static_top, void* top) {
1317 assert(base <= static_top && static_top <= top, "must be");
1318 _shared_metaspace_static_top = static_top;
1319 MetaspaceObj::set_shared_metaspace_range(base, top);
1320 }
1321
1322 bool MetaspaceShared::is_shared_dynamic(void* p) {
1323 if ((p < MetaspaceObj::shared_metaspace_top()) &&
1324 (p >= _shared_metaspace_static_top)) {
1325 return true;
1326 } else {
1327 return false;
1328 }
1329 }
1330
1331 bool MetaspaceShared::is_shared_static(void* p) {
1349 } else if (CDSConfig::new_aot_flags_used()) {
1350 vm_exit_during_initialization("Unable to use AOT cache.", nullptr);
1351 } else {
1352 vm_exit_during_initialization("Unable to use shared archive.", nullptr);
1353 }
1354 }
1355
1356 void MetaspaceShared::report_loading_error(const char* format, ...) {
1357 // When using AOT cache, errors messages are always printed on the error channel.
1358 LogStream ls_aot(LogLevel::Error, LogTagSetMapping<LOG_TAGS(aot)>::tagset());
1359
1360 // If we are loading load the default CDS archive, it may fail due to incompatible VM options.
1361 // Print at the info level to avoid excessive verbosity.
1362 // However, if the user has specified a CDS archive (or AOT cache), they would be interested in
1363 // knowing that the loading fails, so we print at the error level.
1364 LogLevelType level = (!CDSConfig::is_using_archive() || CDSConfig::is_using_only_default_archive()) ?
1365 LogLevel::Info : LogLevel::Error;
1366 LogStream ls_cds(level, LogTagSetMapping<LOG_TAGS(cds)>::tagset());
1367
1368 LogStream& ls = CDSConfig::new_aot_flags_used() ? ls_aot : ls_cds;
1369 if (!ls.is_enabled()) {
1370 return;
1371 }
1372
1373 va_list ap;
1374 va_start(ap, format);
1375
1376 static bool printed_error = false;
1377 if (!printed_error) { // No need for locks. Loading error checks happen only in main thread.
1378 ls.print_cr("An error has occurred while processing the %s. Run with -Xlog:%s for details.",
1379 CDSConfig::type_of_archive_being_loaded(), CDSConfig::new_aot_flags_used() ? "aot" : "aot,cds");
1380 printed_error = true;
1381 }
1382 ls.vprint_cr(format, ap);
1383
1384 va_end(ap);
1385 }
1386
1387 // This function is called when the JVM is unable to write the specified CDS archive due to an
1388 // unrecoverable error.
1389 void MetaspaceShared::unrecoverable_writing_error(const char* message) {
1390 writing_error(message);
1391 vm_direct_exit(1);
1392 }
1393
1394 // This function is called when the JVM is unable to write the specified CDS archive due to a
1395 // an error. The error will be propagated
1396 void MetaspaceShared::writing_error(const char* message) {
1397 aot_log_error(aot)("An error has occurred while writing the shared archive file.");
1398 if (message != nullptr) {
1399 aot_log_error(aot)("%s", message);
1400 }
1401 }
1402
1403 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
1404 assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1405 MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1406
1407 FileMapInfo* static_mapinfo = FileMapInfo::current_info();
1408 FileMapInfo* dynamic_mapinfo = nullptr;
1409
1410 if (static_mapinfo != nullptr) {
1411 aot_log_info(aot)("Core region alignment: %zu", static_mapinfo->core_region_alignment());
1412 dynamic_mapinfo = open_dynamic_archive();
1413
1414 aot_log_info(aot)("ArchiveRelocationMode: %d", ArchiveRelocationMode);
1415
1416 // First try to map at the requested address
1417 result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1418 if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1419 // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1420 // by the OS.
1421 aot_log_info(aot)("Try to map archive(s) at an alternative address");
1422 result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1423 }
1424 }
1425
1426 if (result == MAP_ARCHIVE_SUCCESS) {
1427 bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());
1450 } else {
1451 if (RequireSharedSpaces) {
1452 MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1453 } else {
1454 report_loading_error("Unable to map shared spaces");
1455 }
1456 }
1457 }
1458
1459 // If mapping failed and -XShare:on, the vm should exit
1460 bool has_failed = false;
1461 if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1462 has_failed = true;
1463 delete static_mapinfo;
1464 }
1465 if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1466 has_failed = true;
1467 delete dynamic_mapinfo;
1468 }
1469 if (RequireSharedSpaces && has_failed) {
1470 // static archive mapped but dynamic archive failed
1471 MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
1472 }
1473 }
1474
1475 // This is called very early at VM start up to get the size of the cached_code region
1476 void MetaspaceShared::open_static_archive() {
1477 if (!UseSharedSpaces) { // FIXME -- is this still needed??
1478 return;
1479 }
1480 const char* static_archive = CDSConfig::input_static_archive_path();
1481 assert(static_archive != nullptr, "sanity");
1482 FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1483 if (!mapinfo->open_as_input()) {
1484 delete(mapinfo);
1485 } else {
1486 FileMapRegion* r = mapinfo->region_at(MetaspaceShared::ac);
1487 AOTCacheAccess::set_aot_code_region_size(r->used_aligned());
1488 }
1489 }
1490
1491 FileMapInfo* MetaspaceShared::open_dynamic_archive() {
1492 if (CDSConfig::is_dumping_dynamic_archive()) {
1493 return nullptr;
1494 }
1495 const char* dynamic_archive = CDSConfig::input_dynamic_archive_path();
1496 if (dynamic_archive == nullptr) {
1497 return nullptr;
1498 }
1499
1500 FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1501 if (!mapinfo->open_as_input()) {
1502 delete(mapinfo);
1503 if (RequireSharedSpaces) {
1504 MetaspaceShared::unrecoverable_loading_error("Failed to initialize dynamic archive");
1505 }
1506 return nullptr;
1507 }
1508 return mapinfo;
1954 MemoryReserver::release(archive_space_rs);
1955 archive_space_rs = {};
1956 }
1957 if (class_space_rs.is_reserved()) {
1958 aot_log_debug(aot)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
1959 MemoryReserver::release(class_space_rs);
1960 class_space_rs = {};
1961 }
1962 }
1963 }
1964
1965 static int archive_regions[] = { MetaspaceShared::rw, MetaspaceShared::ro };
1966 static int archive_regions_count = 2;
1967
1968 MapArchiveResult MetaspaceShared::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
1969 assert(CDSConfig::is_using_archive(), "must be runtime");
1970 if (mapinfo == nullptr) {
1971 return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
1972 }
1973
1974 if (!mapinfo->validate_aot_class_linking()) {
1975 return MAP_ARCHIVE_OTHER_FAILURE;
1976 }
1977
1978 mapinfo->set_is_mapped(false);
1979 if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
1980 report_loading_error("Unable to map CDS archive -- core_region_alignment() expected: %zu"
1981 " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
1982 return MAP_ARCHIVE_OTHER_FAILURE;
1983 }
1984
1985 MapArchiveResult result =
1986 mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
1987
1988 if (result != MAP_ARCHIVE_SUCCESS) {
1989 unmap_archive(mapinfo);
1990 return result;
1991 }
1992
1993 if (!mapinfo->validate_class_location()) {
1994 unmap_archive(mapinfo);
1995 return MAP_ARCHIVE_OTHER_FAILURE;
1996 }
1997
2089
2090 if (PrintSharedArchiveAndExit) {
2091 // Print archive names
2092 if (dynamic_mapinfo != nullptr) {
2093 tty->print_cr("\n\nBase archive name: %s", CDSConfig::input_static_archive_path());
2094 tty->print_cr("Base archive version %d", static_mapinfo->version());
2095 } else {
2096 tty->print_cr("Static archive name: %s", static_mapinfo->full_path());
2097 tty->print_cr("Static archive version %d", static_mapinfo->version());
2098 }
2099
2100 SystemDictionaryShared::print_shared_archive(tty);
2101 if (dynamic_mapinfo != nullptr) {
2102 tty->print_cr("\n\nDynamic archive name: %s", dynamic_mapinfo->full_path());
2103 tty->print_cr("Dynamic archive version %d", dynamic_mapinfo->version());
2104 SystemDictionaryShared::print_shared_archive(tty, false/*dynamic*/);
2105 }
2106
2107 TrainingData::print_archived_training_data_on(tty);
2108
2109 AOTCodeCache::print_on(tty);
2110
2111 // collect shared symbols and strings
2112 CountSharedSymbols cl;
2113 SymbolTable::shared_symbols_do(&cl);
2114 tty->print_cr("Number of shared symbols: %d", cl.total());
2115 tty->print_cr("Number of shared strings: %zu", StringTable::shared_entry_count());
2116 tty->print_cr("VM version: %s\r\n", static_mapinfo->vm_version());
2117 if (FileMapInfo::current_info() == nullptr || _archive_loading_failed) {
2118 tty->print_cr("archive is invalid");
2119 vm_exit(1);
2120 } else {
2121 tty->print_cr("archive is valid");
2122 vm_exit(0);
2123 }
2124 }
2125 }
2126
2127 // JVM/TI RedefineClasses() support:
2128 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
2129 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
|