6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "cds/aotArtifactFinder.hpp"
26 #include "cds/aotClassInitializer.hpp"
27 #include "cds/aotClassLinker.hpp"
28 #include "cds/aotClassLocation.hpp"
29 #include "cds/aotConstantPoolResolver.hpp"
30 #include "cds/aotLinkedClassBulkLoader.hpp"
31 #include "cds/aotLogging.hpp"
32 #include "cds/aotMapLogger.hpp"
33 #include "cds/aotMetaspace.hpp"
34 #include "cds/aotReferenceObjSupport.hpp"
35 #include "cds/archiveBuilder.hpp"
36 #include "cds/archiveHeapLoader.hpp"
37 #include "cds/archiveHeapWriter.hpp"
38 #include "cds/cds_globals.hpp"
39 #include "cds/cdsConfig.hpp"
40 #include "cds/cdsProtectionDomain.hpp"
41 #include "cds/classListParser.hpp"
42 #include "cds/classListWriter.hpp"
43 #include "cds/cppVtables.hpp"
44 #include "cds/dumpAllocStats.hpp"
45 #include "cds/dynamicArchive.hpp"
46 #include "cds/filemap.hpp"
47 #include "cds/finalImageRecipes.hpp"
48 #include "cds/heapShared.hpp"
49 #include "cds/lambdaFormInvokers.hpp"
50 #include "cds/lambdaProxyClassDictionary.hpp"
51 #include "classfile/classLoaderDataGraph.hpp"
52 #include "classfile/classLoaderDataShared.hpp"
53 #include "classfile/javaClasses.inline.hpp"
54 #include "classfile/loaderConstraints.hpp"
55 #include "classfile/modules.hpp"
56 #include "classfile/placeholders.hpp"
57 #include "classfile/stringTable.hpp"
58 #include "classfile/symbolTable.hpp"
59 #include "classfile/systemDictionary.hpp"
60 #include "classfile/systemDictionaryShared.hpp"
61 #include "classfile/vmClasses.hpp"
62 #include "classfile/vmSymbols.hpp"
63 #include "code/aotCodeCache.hpp"
64 #include "code/codeCache.hpp"
65 #include "gc/shared/gcVMOperations.hpp"
66 #include "interpreter/bytecodes.hpp"
67 #include "interpreter/bytecodeStream.hpp"
68 #include "jvm_io.h"
69 #include "logging/log.hpp"
70 #include "logging/logMessage.hpp"
71 #include "logging/logStream.hpp"
72 #include "memory/memoryReserver.hpp"
73 #include "memory/metaspace.hpp"
74 #include "memory/metaspaceClosure.hpp"
75 #include "memory/oopFactory.hpp"
76 #include "memory/resourceArea.hpp"
77 #include "memory/universe.hpp"
78 #include "nmt/memTracker.hpp"
79 #include "oops/compressedKlass.hpp"
80 #include "oops/instanceMirrorKlass.hpp"
81 #include "oops/klass.inline.hpp"
82 #include "oops/objArrayOop.hpp"
83 #include "oops/oop.inline.hpp"
84 #include "oops/oopHandle.hpp"
85 #include "oops/trainingData.hpp"
86 #include "prims/jvmtiExport.hpp"
87 #include "runtime/arguments.hpp"
88 #include "runtime/globals.hpp"
89 #include "runtime/globals_extension.hpp"
90 #include "runtime/handles.inline.hpp"
91 #include "runtime/javaCalls.hpp"
92 #include "runtime/os.inline.hpp"
93 #include "runtime/safepointVerifiers.hpp"
94 #include "runtime/sharedRuntime.hpp"
95 #include "runtime/vmOperations.hpp"
96 #include "runtime/vmThread.hpp"
97 #include "sanitizers/leak.hpp"
98 #include "utilities/align.hpp"
99 #include "utilities/bitMap.inline.hpp"
100 #include "utilities/defaultStream.hpp"
101 #include "utilities/hashTable.hpp"
102 #include "utilities/macros.hpp"
103 #include "utilities/ostream.hpp"
104
105 #include <sys/stat.h>
106
107 ReservedSpace AOTMetaspace::_symbol_rs;
108 VirtualSpace AOTMetaspace::_symbol_vs;
109 bool AOTMetaspace::_archive_loading_failed = false;
110 bool AOTMetaspace::_remapped_readwrite = false;
111 void* AOTMetaspace::_aot_metaspace_static_top = nullptr;
112 intx AOTMetaspace::_relocation_delta;
113 char* AOTMetaspace::_requested_base_address;
114 Array<Method*>* AOTMetaspace::_archived_method_handle_intrinsics = nullptr;
115 bool AOTMetaspace::_use_optimized_module_handling = true;
116
117 // The CDS archive is divided into the following regions:
118 // rw - read-write metadata
119 // ro - read-only metadata and read-only tables
120 // hp - heap region
121 // bm - bitmap for relocating the above 7 regions.
122 //
123 // The rw and ro regions are linearly allocated, in the order of rw->ro.
124 // These regions are aligned with AOTMetaspace::core_region_alignment().
125 //
126 // These 2 regions are populated in the following steps:
127 // [0] All classes are loaded in AOTMetaspace::load_classes(). All metadata are
128 // temporarily allocated outside of the shared regions.
129 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
130 // [2] C++ vtables are copied into the rw region.
131 // [3] ArchiveBuilder copies RW metadata into the rw region.
132 // [4] ArchiveBuilder copies RO metadata into the ro region.
133 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
134 // are copied into the ro region as read-only tables.
135 //
276 }
277
278 // Arguments::default_SharedBaseAddress() is hard-coded in cds_globals.hpp. It must be carefully
279 // picked that (a) the align_up() below will always return a valid value; (b) none of
280 // the following asserts will fail.
281 aot_log_warning(aot)("SharedBaseAddress (" INTPTR_FORMAT ") is %s. Reverted to " INTPTR_FORMAT,
282 p2i((void*)SharedBaseAddress), err,
283 p2i((void*)Arguments::default_SharedBaseAddress()));
284
285 specified_base = (char*)Arguments::default_SharedBaseAddress();
286 aligned_base = align_up(specified_base, alignment);
287
288 // Make sure the default value of SharedBaseAddress specified in globals.hpp is sane.
289 assert(!shared_base_too_high(specified_base, aligned_base, cds_max), "Sanity");
290 assert(shared_base_valid(aligned_base), "Sanity");
291 return aligned_base;
292 }
293
294 void AOTMetaspace::initialize_for_static_dump() {
295 assert(CDSConfig::is_dumping_static_archive(), "sanity");
296 aot_log_info(aot)("Core region alignment: %zu", core_region_alignment());
297 // The max allowed size for CDS archive. We use this to limit SharedBaseAddress
298 // to avoid address space wrap around.
299 size_t cds_max;
300 const size_t reserve_alignment = core_region_alignment();
301
302 #ifdef _LP64
303 const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
304 cds_max = align_down(UnscaledClassSpaceMax, reserve_alignment);
305 #else
306 // We don't support archives larger than 256MB on 32-bit due to limited
307 // virtual address space.
308 cds_max = align_down(256*M, reserve_alignment);
309 #endif
310
311 _requested_base_address = compute_shared_base(cds_max);
312 SharedBaseAddress = (size_t)_requested_base_address;
313
314 size_t symbol_rs_size = LP64_ONLY(3 * G) NOT_LP64(128 * M);
315 _symbol_rs = MemoryReserver::reserve(symbol_rs_size,
773 //
774 // It's OK to do this for the built-in loaders as we know they can
775 // tolerate this.
776 return false;
777 }
778 return true;
779 }
780
781 void AOTMetaspace::link_all_loaded_classes(JavaThread* current) {
782 while (true) {
783 ResourceMark rm(current);
784 CollectClassesForLinking collect_classes;
785 bool has_linked = false;
786 const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
787 for (int i = 0; i < mirrors->length(); i++) {
788 OopHandle mirror = mirrors->at(i);
789 InstanceKlass* ik = java_lang_Class::as_InstanceKlass(mirror.resolve());
790 if (may_be_eagerly_linked(ik)) {
791 has_linked |= try_link_class(current, ik);
792 }
793 }
794
795 if (!has_linked) {
796 break;
797 }
798 // Class linking includes verification which may load more classes.
799 // Keep scanning until we have linked no more classes.
800 }
801 }
802
803 void AOTMetaspace::link_shared_classes(TRAPS) {
804 AOTClassLinker::initialize();
805 AOTClassInitializer::init_test_class(CHECK);
806
807 if (CDSConfig::is_dumping_final_static_archive()) {
808 // - Load and link all classes used in the training run.
809 // - Initialize @AOTSafeClassInitializer classes that were
810 // initialized in the training run.
811 // - Perform per-class optimization such as AOT-resolution of
812 // constant pool entries that were resolved during the training run.
815 // Because the AOT assembly phase does not run the same exact code as in the
816 // training run (e.g., we use different lambda form invoker classes;
817 // generated lambda form classes are not recorded in FinalImageRecipes),
818 // the recipes do not cover all classes that have been loaded so far. As
819 // a result, we might have some unlinked classes at this point. Since we
820 // require cached classes to be linked, all such classes will be linked
821 // by the following step.
822 }
823
824 link_all_loaded_classes(THREAD);
825
826 // Eargerly resolve all string constants in constant pools
827 {
828 ResourceMark rm(THREAD);
829 CollectClassesForLinking collect_classes;
830 const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
831 for (int i = 0; i < mirrors->length(); i++) {
832 OopHandle mirror = mirrors->at(i);
833 InstanceKlass* ik = java_lang_Class::as_InstanceKlass(mirror.resolve());
834 AOTConstantPoolResolver::preresolve_string_cp_entries(ik, CHECK);
835 }
836 }
837 }
838
839 void AOTMetaspace::dump_static_archive(TRAPS) {
840 CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
841 ResourceMark rm(THREAD);
842 HandleMark hm(THREAD);
843
844 if (CDSConfig::is_dumping_final_static_archive() && AOTPrintTrainingInfo) {
845 tty->print_cr("==================== archived_training_data ** before dumping ====================");
846 TrainingData::print_archived_training_data_on(tty);
847 }
848
849 StaticArchiveBuilder builder;
850 dump_static_archive_impl(builder, THREAD);
851 if (HAS_PENDING_EXCEPTION) {
852 if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
853 aot_log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
854 "%zuM", MaxHeapSize/M);
855 AOTMetaspace::writing_error();
856 } else {
857 oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
858 aot_log_error(aot)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
859 message == nullptr ? "(null)" : java_lang_String::as_utf8_string(message));
860 AOTMetaspace::writing_error(err_msg("Unexpected exception, use -Xlog:aot%s,exceptions=trace for detail",
861 CDSConfig::new_aot_flags_used() ? "" : ",cds"));
862 }
863 }
864
865 if (CDSConfig::new_aot_flags_used()) {
866 if (CDSConfig::is_dumping_preimage_static_archive()) {
867 // We are in the JVM that runs the training run. Continue execution,
940 }
941 }
942
943 // Some classes are used at CDS runtime but are not yet loaded at this point.
944 // We can perform dummmy calls to these classes at dumptime to ensure they
945 // are archived.
946 exercise_runtime_cds_code(CHECK);
947
948 aot_log_info(aot)("Loading classes to share: done.");
949 }
950
951 void AOTMetaspace::exercise_runtime_cds_code(TRAPS) {
952 // Exercise the manifest processing code
953 const char* dummy = "Manifest-Version: 1.0\n";
954 CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
955
956 // Exercise FileSystem and URL code
957 CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
958 }
959
960 void AOTMetaspace::dump_static_archive_impl(StaticArchiveBuilder& builder, TRAPS) {
961 if (CDSConfig::is_dumping_classic_static_archive()) {
962 // We are running with -Xshare:dump
963 load_classes(CHECK);
964
965 if (SharedArchiveConfigFile) {
966 log_info(aot)("Reading extra data from %s ...", SharedArchiveConfigFile);
967 read_extra_data(THREAD, SharedArchiveConfigFile);
968 log_info(aot)("Reading extra data: done.");
969 }
970 }
971
972 if (CDSConfig::is_dumping_preimage_static_archive()) {
973 log_info(aot)("Reading lambda form invokers from JDK default classlist ...");
974 char default_classlist[JVM_MAXPATHLEN];
975 get_default_classlist(default_classlist, JVM_MAXPATHLEN);
976 struct stat statbuf;
977 if (os::stat(default_classlist, &statbuf) == 0) {
978 ClassListParser::parse_classlist(default_classlist,
979 ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
980 }
1007 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1008 // fails verification, all other interfaces that were not specified in the classlist but
1009 // are implemented by K are not verified.
1010 link_shared_classes(CHECK);
1011 log_info(aot)("Rewriting and linking classes: done");
1012 TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
1013
1014 if (CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
1015 LambdaFormInvokers::regenerate_holder_classes(CHECK);
1016 }
1017
1018 #if INCLUDE_CDS_JAVA_HEAP
1019 if (CDSConfig::is_dumping_heap()) {
1020 ArchiveHeapWriter::init();
1021
1022 if (CDSConfig::is_dumping_full_module_graph()) {
1023 ClassLoaderDataShared::ensure_module_entry_tables_exist();
1024 HeapShared::reset_archived_object_states(CHECK);
1025 }
1026
1027 AOTReferenceObjSupport::initialize(CHECK);
1028 AOTReferenceObjSupport::stabilize_cached_reference_objects(CHECK);
1029
1030 if (CDSConfig::is_initing_classes_at_dump_time()) {
1031 // java.lang.Class::reflectionFactory cannot be archived yet. We set this field
1032 // to null, and it will be initialized again at runtime.
1033 log_debug(aot)("Resetting Class::reflectionFactory");
1034 TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
1035 Symbol* method_sig = vmSymbols::void_method_signature();
1036 JavaValue result(T_VOID);
1037 JavaCalls::call_static(&result, vmClasses::Class_klass(),
1038 method_name, method_sig, CHECK);
1039
1040 // Perhaps there is a way to avoid hard-coding these names here.
1041 // See discussion in JDK-8342481.
1042 }
1043
1044 // Do this at the very end, when no Java code will be executed. Otherwise
1045 // some new strings may be added to the intern table.
1046 StringTable::allocate_shared_strings_array(CHECK);
1047 } else {
1048 log_info(aot)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling");
1049 CDSConfig::stop_using_optimized_module_handling();
1050 }
1051 #endif
1052
1053 VM_PopulateDumpSharedSpace op(builder);
1054 VMThread::execute(&op);
1055
1056 if (AOTCodeCache::is_on_for_dump() && CDSConfig::is_dumping_final_static_archive()) {
1057 CDSConfig::enable_dumping_aot_code();
1058 {
1059 builder.start_ac_region();
1060 // Write the contents to AOT code region and close AOTCodeCache before packing the region
1061 AOTCodeCache::close();
1062 builder.end_ac_region();
1063 }
1064 CDSConfig::disable_dumping_aot_code();
1065 }
1066
1067 bool status = write_static_archive(&builder, op.map_info(), op.heap_info());
1068 if (status && CDSConfig::is_dumping_preimage_static_archive()) {
1069 tty->print_cr("%s AOTConfiguration recorded: %s",
1070 CDSConfig::has_temp_aot_config_file() ? "Temporary" : "", AOTConfiguration);
1071 if (CDSConfig::is_single_command_training()) {
1072 fork_and_dump_final_static_archive(CHECK);
1073 }
1074 }
1075
1076 if (!status) {
1077 THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1078 }
1079 }
1080
1081 bool AOTMetaspace::write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info) {
1082 // relocate the data so that it can be mapped to AOTMetaspace::requested_base_address()
1083 // without runtime relocation.
1084 builder->relocate_to_requested();
1085
1086 map_info->open_as_output();
1087 if (!map_info->is_open()) {
1245 if (HAS_PENDING_EXCEPTION) {
1246 ResourceMark rm(THREAD);
1247 aot_log_warning(aot)("Preload Warning: Verification failed for %s",
1248 ik->external_name());
1249 CLEAR_PENDING_EXCEPTION;
1250 SystemDictionaryShared::set_class_has_failed_verification(ik);
1251 } else {
1252 assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1253 ik->compute_has_loops_flag_for_methods();
1254 }
1255 BytecodeVerificationLocal = saved;
1256 return true;
1257 } else {
1258 return false;
1259 }
1260 }
1261
1262 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1263 if (CDSConfig::is_dumping_heap()) {
1264 HeapShared::write_heap(&_heap_info);
1265 } else {
1266 CDSConfig::log_reasons_for_not_dumping_heap();
1267 }
1268 }
1269
1270 void AOTMetaspace::set_aot_metaspace_range(void* base, void *static_top, void* top) {
1271 assert(base <= static_top && static_top <= top, "must be");
1272 _aot_metaspace_static_top = static_top;
1273 MetaspaceObj::set_aot_metaspace_range(base, top);
1274 }
1275
1276 bool AOTMetaspace::in_aot_cache_dynamic_region(void* p) {
1277 if ((p < MetaspaceObj::aot_metaspace_top()) &&
1278 (p >= _aot_metaspace_static_top)) {
1279 return true;
1280 } else {
1281 return false;
1282 }
1283 }
1284
1285 bool AOTMetaspace::in_aot_cache_static_region(void* p) {
1341 // This function is called when the JVM is unable to write the specified CDS archive due to an
1342 // unrecoverable error.
1343 void AOTMetaspace::unrecoverable_writing_error(const char* message) {
1344 writing_error(message);
1345 vm_direct_exit(1);
1346 }
1347
1348 // This function is called when the JVM is unable to write the specified CDS archive due to a
1349 // an error. The error will be propagated
1350 void AOTMetaspace::writing_error(const char* message) {
1351 aot_log_error(aot)("An error has occurred while writing the shared archive file.");
1352 if (message != nullptr) {
1353 aot_log_error(aot)("%s", message);
1354 }
1355 }
1356
1357 void AOTMetaspace::initialize_runtime_shared_and_meta_spaces() {
1358 assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1359 MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1360
1361 FileMapInfo* static_mapinfo = open_static_archive();
1362 FileMapInfo* dynamic_mapinfo = nullptr;
1363
1364 if (static_mapinfo != nullptr) {
1365 aot_log_info(aot)("Core region alignment: %zu", static_mapinfo->core_region_alignment());
1366 dynamic_mapinfo = open_dynamic_archive();
1367
1368 aot_log_info(aot)("ArchiveRelocationMode: %d", ArchiveRelocationMode);
1369
1370 // First try to map at the requested address
1371 result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1372 if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1373 // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1374 // by the OS.
1375 aot_log_info(aot)("Try to map archive(s) at an alternative address");
1376 result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1377 }
1378 }
1379
1380 if (result == MAP_ARCHIVE_SUCCESS) {
1381 bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());
1404 } else {
1405 if (RequireSharedSpaces) {
1406 AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1407 } else {
1408 report_loading_error("Unable to map shared spaces");
1409 }
1410 }
1411 }
1412
1413 // If mapping failed and -XShare:on, the vm should exit
1414 bool has_failed = false;
1415 if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1416 has_failed = true;
1417 delete static_mapinfo;
1418 }
1419 if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1420 has_failed = true;
1421 delete dynamic_mapinfo;
1422 }
1423 if (RequireSharedSpaces && has_failed) {
1424 AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1425 }
1426 }
1427
1428 FileMapInfo* AOTMetaspace::open_static_archive() {
1429 const char* static_archive = CDSConfig::input_static_archive_path();
1430 assert(static_archive != nullptr, "sanity");
1431 FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1432 if (!mapinfo->open_as_input()) {
1433 delete(mapinfo);
1434 log_info(cds)("Opening of static archive %s failed", static_archive);
1435 return nullptr;
1436 }
1437 return mapinfo;
1438 }
1439
1440 FileMapInfo* AOTMetaspace::open_dynamic_archive() {
1441 if (CDSConfig::is_dumping_dynamic_archive()) {
1442 return nullptr;
1443 }
1444 const char* dynamic_archive = CDSConfig::input_dynamic_archive_path();
1445 if (dynamic_archive == nullptr) {
1446 return nullptr;
1447 }
1448
1449 FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1450 if (!mapinfo->open_as_input()) {
1451 delete(mapinfo);
1452 if (RequireSharedSpaces) {
1453 AOTMetaspace::unrecoverable_loading_error("Failed to initialize dynamic archive");
1454 }
1455 return nullptr;
1456 }
1457 return mapinfo;
1908 MemoryReserver::release(archive_space_rs);
1909 archive_space_rs = {};
1910 }
1911 if (class_space_rs.is_reserved()) {
1912 aot_log_debug(aot)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
1913 MemoryReserver::release(class_space_rs);
1914 class_space_rs = {};
1915 }
1916 }
1917 }
1918
1919 static int archive_regions[] = { AOTMetaspace::rw, AOTMetaspace::ro };
1920 static int archive_regions_count = 2;
1921
1922 MapArchiveResult AOTMetaspace::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
1923 assert(CDSConfig::is_using_archive(), "must be runtime");
1924 if (mapinfo == nullptr) {
1925 return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
1926 }
1927
1928 mapinfo->set_is_mapped(false);
1929 if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
1930 report_loading_error("Unable to map CDS archive -- core_region_alignment() expected: %zu"
1931 " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
1932 return MAP_ARCHIVE_OTHER_FAILURE;
1933 }
1934
1935 MapArchiveResult result =
1936 mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
1937
1938 if (result != MAP_ARCHIVE_SUCCESS) {
1939 unmap_archive(mapinfo);
1940 return result;
1941 }
1942
1943 if (!mapinfo->validate_class_location()) {
1944 unmap_archive(mapinfo);
1945 return MAP_ARCHIVE_OTHER_FAILURE;
1946 }
1947
|
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "cds/aotArtifactFinder.hpp"
26 #include "cds/aotCacheAccess.hpp"
27 #include "cds/aotClassInitializer.hpp"
28 #include "cds/aotClassLinker.hpp"
29 #include "cds/aotClassLocation.hpp"
30 #include "cds/aotConstantPoolResolver.hpp"
31 #include "cds/aotLinkedClassBulkLoader.hpp"
32 #include "cds/aotLogging.hpp"
33 #include "cds/aotMapLogger.hpp"
34 #include "cds/aotMetaspace.hpp"
35 #include "cds/aotReferenceObjSupport.hpp"
36 #include "cds/archiveBuilder.hpp"
37 #include "cds/archiveHeapLoader.hpp"
38 #include "cds/archiveHeapWriter.hpp"
39 #include "cds/cds_globals.hpp"
40 #include "cds/cdsConfig.hpp"
41 #include "cds/cdsProtectionDomain.hpp"
42 #include "cds/classListParser.hpp"
43 #include "cds/classListWriter.hpp"
44 #include "cds/cppVtables.hpp"
45 #include "cds/dumpAllocStats.hpp"
46 #include "cds/dynamicArchive.hpp"
47 #include "cds/filemap.hpp"
48 #include "cds/finalImageRecipes.hpp"
49 #include "cds/heapShared.hpp"
50 #include "cds/lambdaFormInvokers.hpp"
51 #include "cds/lambdaProxyClassDictionary.hpp"
52 #include "classfile/classLoaderDataGraph.hpp"
53 #include "classfile/classLoaderDataShared.hpp"
54 #include "classfile/javaClasses.inline.hpp"
55 #include "classfile/loaderConstraints.hpp"
56 #include "classfile/modules.hpp"
57 #include "classfile/placeholders.hpp"
58 #include "classfile/stringTable.hpp"
59 #include "classfile/symbolTable.hpp"
60 #include "classfile/systemDictionary.hpp"
61 #include "classfile/systemDictionaryShared.hpp"
62 #include "classfile/vmClasses.hpp"
63 #include "classfile/vmSymbols.hpp"
64 #include "code/aotCodeCache.hpp"
65 #include "code/codeCache.hpp"
66 #include "compiler/compileBroker.hpp"
67 #include "compiler/precompiler.hpp"
68 #include "gc/shared/gcVMOperations.hpp"
69 #include "interpreter/bytecodes.hpp"
70 #include "interpreter/bytecodeStream.hpp"
71 #include "jvm_io.h"
72 #include "logging/log.hpp"
73 #include "logging/logMessage.hpp"
74 #include "logging/logStream.hpp"
75 #include "memory/memoryReserver.hpp"
76 #include "memory/metaspace.hpp"
77 #include "memory/metaspaceClosure.hpp"
78 #include "memory/oopFactory.hpp"
79 #include "memory/resourceArea.hpp"
80 #include "memory/universe.hpp"
81 #include "nmt/memTracker.hpp"
82 #include "oops/compressedKlass.hpp"
83 #include "oops/instanceMirrorKlass.hpp"
84 #include "oops/klass.inline.hpp"
85 #include "oops/method.inline.hpp"
86 #include "oops/objArrayOop.hpp"
87 #include "oops/oop.inline.hpp"
88 #include "oops/oopHandle.hpp"
89 #include "oops/trainingData.hpp"
90 #include "prims/jvmtiExport.hpp"
91 #include "prims/whitebox.hpp"
92 #include "runtime/arguments.hpp"
93 #include "runtime/globals.hpp"
94 #include "runtime/globals_extension.hpp"
95 #include "runtime/handles.inline.hpp"
96 #include "runtime/javaCalls.hpp"
97 #include "runtime/os.inline.hpp"
98 #include "runtime/safepointVerifiers.hpp"
99 #include "runtime/sharedRuntime.hpp"
100 #include "runtime/vmOperations.hpp"
101 #include "runtime/vmThread.hpp"
102 #include "sanitizers/leak.hpp"
103 #include "services/management.hpp"
104 #include "utilities/align.hpp"
105 #include "utilities/bitMap.inline.hpp"
106 #include "utilities/defaultStream.hpp"
107 #include "utilities/hashTable.hpp"
108 #include "utilities/macros.hpp"
109 #include "utilities/ostream.hpp"
110
111 #include <sys/stat.h>
112
113 ReservedSpace AOTMetaspace::_symbol_rs;
114 VirtualSpace AOTMetaspace::_symbol_vs;
115 bool AOTMetaspace::_archive_loading_failed = false;
116 bool AOTMetaspace::_remapped_readwrite = false;
117 void* AOTMetaspace::_aot_metaspace_static_top = nullptr;
118 intx AOTMetaspace::_relocation_delta;
119 char* AOTMetaspace::_requested_base_address;
120 Array<Method*>* AOTMetaspace::_archived_method_handle_intrinsics = nullptr;
121 bool AOTMetaspace::_use_optimized_module_handling = true;
122 int volatile AOTMetaspace::_preimage_static_archive_dumped = 0;
123 jlong AOTMetaspace::_preimage_static_archive_recording_duration = 0;
124
125 // The CDS archive is divided into the following regions:
126 // rw - read-write metadata
127 // ro - read-only metadata and read-only tables
128 // hp - heap region
129 // bm - bitmap for relocating the above 7 regions.
130 //
131 // The rw and ro regions are linearly allocated, in the order of rw->ro.
132 // These regions are aligned with AOTMetaspace::core_region_alignment().
133 //
134 // These 2 regions are populated in the following steps:
135 // [0] All classes are loaded in AOTMetaspace::load_classes(). All metadata are
136 // temporarily allocated outside of the shared regions.
137 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
138 // [2] C++ vtables are copied into the rw region.
139 // [3] ArchiveBuilder copies RW metadata into the rw region.
140 // [4] ArchiveBuilder copies RO metadata into the ro region.
141 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
142 // are copied into the ro region as read-only tables.
143 //
284 }
285
286 // Arguments::default_SharedBaseAddress() is hard-coded in cds_globals.hpp. It must be carefully
287 // picked that (a) the align_up() below will always return a valid value; (b) none of
288 // the following asserts will fail.
289 aot_log_warning(aot)("SharedBaseAddress (" INTPTR_FORMAT ") is %s. Reverted to " INTPTR_FORMAT,
290 p2i((void*)SharedBaseAddress), err,
291 p2i((void*)Arguments::default_SharedBaseAddress()));
292
293 specified_base = (char*)Arguments::default_SharedBaseAddress();
294 aligned_base = align_up(specified_base, alignment);
295
296 // Make sure the default value of SharedBaseAddress specified in globals.hpp is sane.
297 assert(!shared_base_too_high(specified_base, aligned_base, cds_max), "Sanity");
298 assert(shared_base_valid(aligned_base), "Sanity");
299 return aligned_base;
300 }
301
302 void AOTMetaspace::initialize_for_static_dump() {
303 assert(CDSConfig::is_dumping_static_archive(), "sanity");
304
305 if (CDSConfig::is_dumping_preimage_static_archive() || CDSConfig::is_dumping_final_static_archive()) {
306 if (!((UseG1GC || UseParallelGC || UseSerialGC || UseEpsilonGC || UseShenandoahGC) && UseCompressedClassPointers)) {
307 const char* error;
308 if (CDSConfig::is_dumping_preimage_static_archive()) {
309 error = "Cannot create the AOT configuration file";
310 } else {
311 error = "Cannot create the AOT cache";
312 }
313
314 vm_exit_during_initialization(error,
315 "UseCompressedClassPointers must be enabled, and collector must be G1, Parallel, Serial, Epsilon, or Shenandoah");
316 }
317 }
318
319 aot_log_info(aot)("Core region alignment: %zu", core_region_alignment());
320 // The max allowed size for CDS archive. We use this to limit SharedBaseAddress
321 // to avoid address space wrap around.
322 size_t cds_max;
323 const size_t reserve_alignment = core_region_alignment();
324
325 #ifdef _LP64
326 const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
327 cds_max = align_down(UnscaledClassSpaceMax, reserve_alignment);
328 #else
329 // We don't support archives larger than 256MB on 32-bit due to limited
330 // virtual address space.
331 cds_max = align_down(256*M, reserve_alignment);
332 #endif
333
334 _requested_base_address = compute_shared_base(cds_max);
335 SharedBaseAddress = (size_t)_requested_base_address;
336
337 size_t symbol_rs_size = LP64_ONLY(3 * G) NOT_LP64(128 * M);
338 _symbol_rs = MemoryReserver::reserve(symbol_rs_size,
796 //
797 // It's OK to do this for the built-in loaders as we know they can
798 // tolerate this.
799 return false;
800 }
801 return true;
802 }
803
804 void AOTMetaspace::link_all_loaded_classes(JavaThread* current) {
805 while (true) {
806 ResourceMark rm(current);
807 CollectClassesForLinking collect_classes;
808 bool has_linked = false;
809 const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
810 for (int i = 0; i < mirrors->length(); i++) {
811 OopHandle mirror = mirrors->at(i);
812 InstanceKlass* ik = java_lang_Class::as_InstanceKlass(mirror.resolve());
813 if (may_be_eagerly_linked(ik)) {
814 has_linked |= try_link_class(current, ik);
815 }
816 if (CDSConfig::is_dumping_heap() && ik->is_linked() && !ik->is_initialized()) {
817 AOTClassInitializer::maybe_preinit_class(ik, current);
818 }
819 }
820
821 if (!has_linked) {
822 break;
823 }
824 // Class linking includes verification which may load more classes.
825 // Keep scanning until we have linked no more classes.
826 }
827 }
828
829 void AOTMetaspace::link_shared_classes(TRAPS) {
830 AOTClassLinker::initialize();
831 AOTClassInitializer::init_test_class(CHECK);
832
833 if (CDSConfig::is_dumping_final_static_archive()) {
834 // - Load and link all classes used in the training run.
835 // - Initialize @AOTSafeClassInitializer classes that were
836 // initialized in the training run.
837 // - Perform per-class optimization such as AOT-resolution of
838 // constant pool entries that were resolved during the training run.
841 // Because the AOT assembly phase does not run the same exact code as in the
842 // training run (e.g., we use different lambda form invoker classes;
843 // generated lambda form classes are not recorded in FinalImageRecipes),
844 // the recipes do not cover all classes that have been loaded so far. As
845 // a result, we might have some unlinked classes at this point. Since we
846 // require cached classes to be linked, all such classes will be linked
847 // by the following step.
848 }
849
850 link_all_loaded_classes(THREAD);
851
852 // Eargerly resolve all string constants in constant pools
853 {
854 ResourceMark rm(THREAD);
855 CollectClassesForLinking collect_classes;
856 const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
857 for (int i = 0; i < mirrors->length(); i++) {
858 OopHandle mirror = mirrors->at(i);
859 InstanceKlass* ik = java_lang_Class::as_InstanceKlass(mirror.resolve());
860 AOTConstantPoolResolver::preresolve_string_cp_entries(ik, CHECK);
861 if (CDSConfig::is_dumping_preimage_static_archive()) {
862 FinalImageRecipes::add_reflection_data_flags(ik, CHECK);
863 }
864 }
865 }
866 }
867
868 void AOTMetaspace::dump_static_archive(TRAPS) {
869 CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
870 ResourceMark rm(THREAD);
871 HandleMark hm(THREAD);
872
873 if (CDSConfig::is_dumping_final_static_archive() && AOTPrintTrainingInfo) {
874 tty->print_cr("==================== archived_training_data ** before dumping ====================");
875 TrainingData::print_archived_training_data_on(tty);
876 }
877
878 StaticArchiveBuilder builder;
879 dump_static_archive_impl(builder, THREAD);
880 if (HAS_PENDING_EXCEPTION) {
881 if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
882 aot_log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
883 "%zuM", MaxHeapSize/M);
884 AOTMetaspace::writing_error();
885 } else {
886 oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
887 aot_log_error(aot)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
888 message == nullptr ? "(null)" : java_lang_String::as_utf8_string(message));
889 AOTMetaspace::writing_error(err_msg("Unexpected exception, use -Xlog:aot%s,exceptions=trace for detail",
890 CDSConfig::new_aot_flags_used() ? "" : ",cds"));
891 }
892 }
893
894 if (CDSConfig::new_aot_flags_used()) {
895 if (CDSConfig::is_dumping_preimage_static_archive()) {
896 // We are in the JVM that runs the training run. Continue execution,
969 }
970 }
971
972 // Some classes are used at CDS runtime but are not yet loaded at this point.
973 // We can perform dummmy calls to these classes at dumptime to ensure they
974 // are archived.
975 exercise_runtime_cds_code(CHECK);
976
977 aot_log_info(aot)("Loading classes to share: done.");
978 }
979
980 void AOTMetaspace::exercise_runtime_cds_code(TRAPS) {
981 // Exercise the manifest processing code
982 const char* dummy = "Manifest-Version: 1.0\n";
983 CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
984
985 // Exercise FileSystem and URL code
986 CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
987 }
988
989 bool AOTMetaspace::is_recording_preimage_static_archive() {
990 if (CDSConfig::is_dumping_preimage_static_archive()) {
991 return _preimage_static_archive_dumped == 0;
992 }
993 return false;
994 }
995
996 jlong AOTMetaspace::get_preimage_static_archive_recording_duration() {
997 if (CDSConfig::is_dumping_preimage_static_archive()) {
998 if (_preimage_static_archive_recording_duration == 0) {
999 // The recording has not yet finished so return the current elapsed time.
1000 return Management::ticks_to_ms(os::elapsed_counter());
1001 }
1002 return _preimage_static_archive_recording_duration;
1003 }
1004 return 0;
1005 }
1006
1007 void AOTMetaspace::dump_static_archive_impl(StaticArchiveBuilder& builder, TRAPS) {
1008 if (CDSConfig::is_dumping_preimage_static_archive()) {
1009 if (AtomicAccess::cmpxchg(&_preimage_static_archive_dumped, 0, 1) != 0) {
1010 return;
1011 }
1012 _preimage_static_archive_recording_duration = Management::ticks_to_ms(os::elapsed_counter());
1013 }
1014
1015 if (CDSConfig::is_dumping_classic_static_archive()) {
1016 // We are running with -Xshare:dump
1017 load_classes(CHECK);
1018
1019 if (SharedArchiveConfigFile) {
1020 log_info(aot)("Reading extra data from %s ...", SharedArchiveConfigFile);
1021 read_extra_data(THREAD, SharedArchiveConfigFile);
1022 log_info(aot)("Reading extra data: done.");
1023 }
1024 }
1025
1026 if (CDSConfig::is_dumping_preimage_static_archive()) {
1027 log_info(aot)("Reading lambda form invokers from JDK default classlist ...");
1028 char default_classlist[JVM_MAXPATHLEN];
1029 get_default_classlist(default_classlist, JVM_MAXPATHLEN);
1030 struct stat statbuf;
1031 if (os::stat(default_classlist, &statbuf) == 0) {
1032 ClassListParser::parse_classlist(default_classlist,
1033 ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
1034 }
1061 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1062 // fails verification, all other interfaces that were not specified in the classlist but
1063 // are implemented by K are not verified.
1064 link_shared_classes(CHECK);
1065 log_info(aot)("Rewriting and linking classes: done");
1066 TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
1067
1068 if (CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
1069 LambdaFormInvokers::regenerate_holder_classes(CHECK);
1070 }
1071
1072 #if INCLUDE_CDS_JAVA_HEAP
1073 if (CDSConfig::is_dumping_heap()) {
1074 ArchiveHeapWriter::init();
1075
1076 if (CDSConfig::is_dumping_full_module_graph()) {
1077 ClassLoaderDataShared::ensure_module_entry_tables_exist();
1078 HeapShared::reset_archived_object_states(CHECK);
1079 }
1080
1081 if (ArchiveLoaderLookupCache) {
1082 SystemDictionaryShared::create_loader_positive_lookup_cache(CHECK);
1083 }
1084
1085 AOTReferenceObjSupport::initialize(CHECK);
1086 AOTReferenceObjSupport::stabilize_cached_reference_objects(CHECK);
1087
1088 if (CDSConfig::is_initing_classes_at_dump_time()) {
1089 // java.lang.Class::reflectionFactory cannot be archived yet. We set this field
1090 // to null, and it will be initialized again at runtime.
1091 log_debug(aot)("Resetting Class::reflectionFactory");
1092 TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
1093 Symbol* method_sig = vmSymbols::void_method_signature();
1094 JavaValue result(T_VOID);
1095 JavaCalls::call_static(&result, vmClasses::Class_klass(),
1096 method_name, method_sig, CHECK);
1097
1098 // Perhaps there is a way to avoid hard-coding these names here.
1099 // See discussion in JDK-8342481.
1100 }
1101
1102 // Do this at the very end, when no Java code will be executed. Otherwise
1103 // some new strings may be added to the intern table.
1104 StringTable::allocate_shared_strings_array(CHECK);
1105 } else {
1106 log_info(aot)("Not dumping heap, reset CDSConfig::_is_using_optimized_module_handling");
1107 CDSConfig::stop_using_optimized_module_handling();
1108 }
1109 #endif
1110
1111 VM_PopulateDumpSharedSpace op(builder);
1112 VMThread::execute(&op);
1113 FileMapInfo* mapinfo = op.map_info();
1114 ArchiveHeapInfo* heap_info = op.heap_info();
1115
1116 if (CDSConfig::is_dumping_final_static_archive()) {
1117 if (AOTCodeCache::is_caching_enabled()) {
1118 if (log_is_enabled(Info, cds, jit)) {
1119 AOTCacheAccess::test_heap_access_api();
1120 }
1121
1122 // We have just created the final image. Let's run the AOT compiler
1123 if (AOTPrintTrainingInfo) {
1124 tty->print_cr("==================== archived_training_data ** after dumping ====================");
1125 TrainingData::print_archived_training_data_on(tty);
1126 }
1127
1128 {
1129 builder.start_ac_region();
1130 if (AOTCodeCache::is_dumping_code()) {
1131 CDSConfig::enable_dumping_aot_code();
1132 log_info(aot)("Compiling AOT code");
1133 Precompiler::compile_aot_code(&builder, CHECK);
1134 log_info(aot)("Finished compiling AOT code");
1135 CDSConfig::disable_dumping_aot_code();
1136 }
1137 // Write the contents to aot code region and close AOTCodeCache before packing the region
1138 AOTCodeCache::close();
1139 log_info(aot)("Dumped AOT code Cache");
1140 builder.end_ac_region();
1141 }
1142 }
1143 }
1144
1145 bool status = write_static_archive(&builder, mapinfo, heap_info);
1146 if (status && CDSConfig::is_dumping_preimage_static_archive()) {
1147 tty->print_cr("%s AOTConfiguration recorded: %s",
1148 CDSConfig::has_temp_aot_config_file() ? "Temporary" : "", AOTConfiguration);
1149 if (CDSConfig::is_single_command_training()) {
1150 fork_and_dump_final_static_archive(CHECK);
1151 }
1152 }
1153
1154 if (!status) {
1155 THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
1156 }
1157 }
1158
1159 bool AOTMetaspace::write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info) {
1160 // relocate the data so that it can be mapped to AOTMetaspace::requested_base_address()
1161 // without runtime relocation.
1162 builder->relocate_to_requested();
1163
1164 map_info->open_as_output();
1165 if (!map_info->is_open()) {
1323 if (HAS_PENDING_EXCEPTION) {
1324 ResourceMark rm(THREAD);
1325 aot_log_warning(aot)("Preload Warning: Verification failed for %s",
1326 ik->external_name());
1327 CLEAR_PENDING_EXCEPTION;
1328 SystemDictionaryShared::set_class_has_failed_verification(ik);
1329 } else {
1330 assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1331 ik->compute_has_loops_flag_for_methods();
1332 }
1333 BytecodeVerificationLocal = saved;
1334 return true;
1335 } else {
1336 return false;
1337 }
1338 }
1339
1340 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1341 if (CDSConfig::is_dumping_heap()) {
1342 HeapShared::write_heap(&_heap_info);
1343 } else if (!CDSConfig::is_dumping_preimage_static_archive()) {
1344 CDSConfig::log_reasons_for_not_dumping_heap();
1345 }
1346 }
1347
1348 void AOTMetaspace::set_aot_metaspace_range(void* base, void *static_top, void* top) {
1349 assert(base <= static_top && static_top <= top, "must be");
1350 _aot_metaspace_static_top = static_top;
1351 MetaspaceObj::set_aot_metaspace_range(base, top);
1352 }
1353
1354 bool AOTMetaspace::in_aot_cache_dynamic_region(void* p) {
1355 if ((p < MetaspaceObj::aot_metaspace_top()) &&
1356 (p >= _aot_metaspace_static_top)) {
1357 return true;
1358 } else {
1359 return false;
1360 }
1361 }
1362
1363 bool AOTMetaspace::in_aot_cache_static_region(void* p) {
1419 // This function is called when the JVM is unable to write the specified CDS archive due to an
1420 // unrecoverable error.
1421 void AOTMetaspace::unrecoverable_writing_error(const char* message) {
1422 writing_error(message);
1423 vm_direct_exit(1);
1424 }
1425
1426 // This function is called when the JVM is unable to write the specified CDS archive due to a
1427 // an error. The error will be propagated
1428 void AOTMetaspace::writing_error(const char* message) {
1429 aot_log_error(aot)("An error has occurred while writing the shared archive file.");
1430 if (message != nullptr) {
1431 aot_log_error(aot)("%s", message);
1432 }
1433 }
1434
1435 void AOTMetaspace::initialize_runtime_shared_and_meta_spaces() {
1436 assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
1437 MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
1438
1439 FileMapInfo* static_mapinfo = FileMapInfo::current_info();
1440 FileMapInfo* dynamic_mapinfo = nullptr;
1441
1442 if (static_mapinfo != nullptr) {
1443 aot_log_info(aot)("Core region alignment: %zu", static_mapinfo->core_region_alignment());
1444 dynamic_mapinfo = open_dynamic_archive();
1445
1446 aot_log_info(aot)("ArchiveRelocationMode: %d", ArchiveRelocationMode);
1447
1448 // First try to map at the requested address
1449 result = map_archives(static_mapinfo, dynamic_mapinfo, true);
1450 if (result == MAP_ARCHIVE_MMAP_FAILURE) {
1451 // Mapping has failed (probably due to ASLR). Let's map at an address chosen
1452 // by the OS.
1453 aot_log_info(aot)("Try to map archive(s) at an alternative address");
1454 result = map_archives(static_mapinfo, dynamic_mapinfo, false);
1455 }
1456 }
1457
1458 if (result == MAP_ARCHIVE_SUCCESS) {
1459 bool dynamic_mapped = (dynamic_mapinfo != nullptr && dynamic_mapinfo->is_mapped());
1482 } else {
1483 if (RequireSharedSpaces) {
1484 AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1485 } else {
1486 report_loading_error("Unable to map shared spaces");
1487 }
1488 }
1489 }
1490
1491 // If mapping failed and -XShare:on, the vm should exit
1492 bool has_failed = false;
1493 if (static_mapinfo != nullptr && !static_mapinfo->is_mapped()) {
1494 has_failed = true;
1495 delete static_mapinfo;
1496 }
1497 if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
1498 has_failed = true;
1499 delete dynamic_mapinfo;
1500 }
1501 if (RequireSharedSpaces && has_failed) {
1502 // static archive mapped but dynamic archive failed
1503 AOTMetaspace::unrecoverable_loading_error("Unable to map shared spaces");
1504 }
1505 }
1506
1507 // This is called very early at VM start up to get the size of the cached_code region
1508 void AOTMetaspace::open_static_archive() {
1509 if (!UseSharedSpaces) { // FIXME -- is this still needed??
1510 return;
1511 }
1512 const char* static_archive = CDSConfig::input_static_archive_path();
1513 assert(static_archive != nullptr, "sanity");
1514 FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1515 if (!mapinfo->open_as_input()) {
1516 delete(mapinfo);
1517 log_info(cds)("Opening of static archive %s failed", static_archive);
1518 } else {
1519 FileMapRegion* r = mapinfo->region_at(AOTMetaspace::ac);
1520 AOTCacheAccess::set_aot_code_region_size(r->used_aligned());
1521 }
1522 }
1523
1524 FileMapInfo* AOTMetaspace::open_dynamic_archive() {
1525 if (CDSConfig::is_dumping_dynamic_archive()) {
1526 return nullptr;
1527 }
1528 const char* dynamic_archive = CDSConfig::input_dynamic_archive_path();
1529 if (dynamic_archive == nullptr) {
1530 return nullptr;
1531 }
1532
1533 FileMapInfo* mapinfo = new FileMapInfo(dynamic_archive, false);
1534 if (!mapinfo->open_as_input()) {
1535 delete(mapinfo);
1536 if (RequireSharedSpaces) {
1537 AOTMetaspace::unrecoverable_loading_error("Failed to initialize dynamic archive");
1538 }
1539 return nullptr;
1540 }
1541 return mapinfo;
1992 MemoryReserver::release(archive_space_rs);
1993 archive_space_rs = {};
1994 }
1995 if (class_space_rs.is_reserved()) {
1996 aot_log_debug(aot)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base()));
1997 MemoryReserver::release(class_space_rs);
1998 class_space_rs = {};
1999 }
2000 }
2001 }
2002
2003 static int archive_regions[] = { AOTMetaspace::rw, AOTMetaspace::ro };
2004 static int archive_regions_count = 2;
2005
2006 MapArchiveResult AOTMetaspace::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) {
2007 assert(CDSConfig::is_using_archive(), "must be runtime");
2008 if (mapinfo == nullptr) {
2009 return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
2010 }
2011
2012 if (!mapinfo->validate_aot_class_linking()) {
2013 return MAP_ARCHIVE_OTHER_FAILURE;
2014 }
2015
2016 mapinfo->set_is_mapped(false);
2017 if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
2018 report_loading_error("Unable to map CDS archive -- core_region_alignment() expected: %zu"
2019 " actual: %zu", mapinfo->core_region_alignment(), core_region_alignment());
2020 return MAP_ARCHIVE_OTHER_FAILURE;
2021 }
2022
2023 MapArchiveResult result =
2024 mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs);
2025
2026 if (result != MAP_ARCHIVE_SUCCESS) {
2027 unmap_archive(mapinfo);
2028 return result;
2029 }
2030
2031 if (!mapinfo->validate_class_location()) {
2032 unmap_archive(mapinfo);
2033 return MAP_ARCHIVE_OTHER_FAILURE;
2034 }
2035
|