< prev index next >

src/hotspot/share/cds/aotMetaspace.cpp

Print this page

   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "cds/aotArtifactFinder.hpp"

  26 #include "cds/aotClassInitializer.hpp"
  27 #include "cds/aotClassLinker.hpp"
  28 #include "cds/aotClassLocation.hpp"
  29 #include "cds/aotConstantPoolResolver.hpp"
  30 #include "cds/aotLinkedClassBulkLoader.hpp"
  31 #include "cds/aotLogging.hpp"
  32 #include "cds/aotMapLogger.hpp"
  33 #include "cds/aotMetaspace.hpp"
  34 #include "cds/aotReferenceObjSupport.hpp"
  35 #include "cds/archiveBuilder.hpp"
  36 #include "cds/archiveHeapLoader.hpp"
  37 #include "cds/archiveHeapWriter.hpp"
  38 #include "cds/cds_globals.hpp"
  39 #include "cds/cdsConfig.hpp"
  40 #include "cds/cdsProtectionDomain.hpp"
  41 #include "cds/classListParser.hpp"
  42 #include "cds/classListWriter.hpp"
  43 #include "cds/cppVtables.hpp"
  44 #include "cds/dumpAllocStats.hpp"
  45 #include "cds/dynamicArchive.hpp"
  46 #include "cds/filemap.hpp"
  47 #include "cds/finalImageRecipes.hpp"
  48 #include "cds/heapShared.hpp"
  49 #include "cds/lambdaFormInvokers.hpp"
  50 #include "cds/lambdaProxyClassDictionary.hpp"
  51 #include "classfile/classLoaderDataGraph.hpp"
  52 #include "classfile/classLoaderDataShared.hpp"
  53 #include "classfile/javaClasses.inline.hpp"
  54 #include "classfile/loaderConstraints.hpp"
  55 #include "classfile/modules.hpp"
  56 #include "classfile/placeholders.hpp"
  57 #include "classfile/stringTable.hpp"
  58 #include "classfile/symbolTable.hpp"
  59 #include "classfile/systemDictionary.hpp"
  60 #include "classfile/systemDictionaryShared.hpp"
  61 #include "classfile/vmClasses.hpp"
  62 #include "classfile/vmSymbols.hpp"
  63 #include "code/aotCodeCache.hpp"
  64 #include "code/codeCache.hpp"


  65 #include "gc/shared/gcVMOperations.hpp"
  66 #include "interpreter/bytecodes.hpp"
  67 #include "interpreter/bytecodeStream.hpp"
  68 #include "jvm_io.h"
  69 #include "logging/log.hpp"
  70 #include "logging/logMessage.hpp"
  71 #include "logging/logStream.hpp"
  72 #include "memory/memoryReserver.hpp"
  73 #include "memory/metaspace.hpp"
  74 #include "memory/metaspaceClosure.hpp"
  75 #include "memory/oopFactory.hpp"
  76 #include "memory/resourceArea.hpp"
  77 #include "memory/universe.hpp"
  78 #include "nmt/memTracker.hpp"
  79 #include "oops/compressedKlass.hpp"
  80 #include "oops/instanceMirrorKlass.hpp"
  81 #include "oops/klass.inline.hpp"

  82 #include "oops/objArrayOop.hpp"
  83 #include "oops/oop.inline.hpp"
  84 #include "oops/oopHandle.hpp"
  85 #include "oops/trainingData.hpp"
  86 #include "prims/jvmtiExport.hpp"

  87 #include "runtime/arguments.hpp"
  88 #include "runtime/globals.hpp"
  89 #include "runtime/globals_extension.hpp"
  90 #include "runtime/handles.inline.hpp"
  91 #include "runtime/javaCalls.hpp"
  92 #include "runtime/os.inline.hpp"
  93 #include "runtime/safepointVerifiers.hpp"
  94 #include "runtime/sharedRuntime.hpp"
  95 #include "runtime/vmOperations.hpp"
  96 #include "runtime/vmThread.hpp"
  97 #include "sanitizers/leak.hpp"

  98 #include "utilities/align.hpp"
  99 #include "utilities/bitMap.inline.hpp"
 100 #include "utilities/defaultStream.hpp"
 101 #include "utilities/hashTable.hpp"
 102 #include "utilities/macros.hpp"
 103 #include "utilities/ostream.hpp"
 104 
 105 #include <sys/stat.h>
 106 
 107 ReservedSpace AOTMetaspace::_symbol_rs;
 108 VirtualSpace AOTMetaspace::_symbol_vs;
 109 bool AOTMetaspace::_archive_loading_failed = false;
 110 bool AOTMetaspace::_remapped_readwrite = false;
 111 void* AOTMetaspace::_aot_metaspace_static_top = nullptr;
 112 intx AOTMetaspace::_relocation_delta;
 113 char* AOTMetaspace::_requested_base_address;
 114 Array<Method*>* AOTMetaspace::_archived_method_handle_intrinsics = nullptr;
 115 bool AOTMetaspace::_use_optimized_module_handling = true;


 116 
 117 // The CDS archive is divided into the following regions:
 118 //     rw  - read-write metadata
 119 //     ro  - read-only metadata and read-only tables
 120 //     hp  - heap region
 121 //     bm  - bitmap for relocating the above 7 regions.
 122 //
 123 // The rw and ro regions are linearly allocated, in the order of rw->ro.
 124 // These regions are aligned with AOTMetaspace::core_region_alignment().
 125 //
 126 // These 2 regions are populated in the following steps:
 127 // [0] All classes are loaded in AOTMetaspace::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 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,

 700 
 701   log_info(cds)("Make training data shareable");
 702   _builder.make_training_data_shareable();
 703 
 704   // The vtable clones contain addresses of the current process.
 705   // We don't want to write these addresses into the archive.
 706   CppVtables::zero_archived_vtables();
 707 
 708   // Write the archive file
 709   if (CDSConfig::is_dumping_final_static_archive()) {
 710     FileMapInfo::free_current_info(); // FIXME: should not free current info
 711   }
 712   const char* static_archive = CDSConfig::output_archive_path();
 713   assert(static_archive != nullptr, "sanity");
 714   _map_info = new FileMapInfo(static_archive, true);
 715   _map_info->populate_header(AOTMetaspace::core_region_alignment());
 716   _map_info->set_early_serialized_data(early_serialized_data);
 717   _map_info->set_serialized_data(serialized_data);
 718   _map_info->set_cloned_vtables(CppVtables::vtables_serialized_base());
 719   _map_info->header()->set_class_location_config(cl_config);


 720 }
 721 
 722 class CollectClassesForLinking : public KlassClosure {
 723   GrowableArray<OopHandle> _mirrors;
 724 
 725 public:
 726    CollectClassesForLinking() : _mirrors() {
 727      // ClassLoaderDataGraph::loaded_classes_do_keepalive() requires ClassLoaderDataGraph_lock.
 728      // We cannot link the classes while holding this lock (or else we may run into deadlock).
 729      // Therefore, we need to first collect all the classes, keeping them alive by
 730      // holding onto their java_mirrors in global OopHandles. We then link the classes after
 731      // releasing the lock.
 732      MutexLocker lock(ClassLoaderDataGraph_lock);
 733      ClassLoaderDataGraph::loaded_classes_do_keepalive(this);
 734    }
 735 
 736   ~CollectClassesForLinking() {
 737     for (int i = 0; i < _mirrors.length(); i++) {
 738       _mirrors.at(i).release(Universe::vm_global());
 739     }
 740   }
 741 
 742   void do_cld(ClassLoaderData* cld) {
 743     assert(cld->is_alive(), "must be");
 744   }
 745 
 746   void do_klass(Klass* k) {
 747     if (k->is_instance_klass()) {
 748       _mirrors.append(OopHandle(Universe::vm_global(), k->java_mirror()));
 749     }
 750   }
 751 
 752   const GrowableArray<OopHandle>* mirrors() const { return &_mirrors; }
 753 };
 754 
 755 // Check if we can eagerly link this class at dump time, so we can avoid the
 756 // runtime linking overhead (especially verification)
 757 bool AOTMetaspace::may_be_eagerly_linked(InstanceKlass* ik) {



 758   if (!ik->can_be_verified_at_dumptime()) {
 759     // For old classes, try to leave them in the unlinked state, so
 760     // we can still store them in the archive. They must be
 761     // linked/verified at runtime.
 762     return false;
 763   }
 764   if (CDSConfig::is_dumping_dynamic_archive() && ik->defined_by_other_loaders()) {
 765     // Linking of unregistered classes at this stage may cause more
 766     // classes to be resolved, resulting in calls to ClassLoader.loadClass()
 767     // that may not be expected by custom class loaders.
 768     //
 769     // It's OK to do this for the built-in loaders as we know they can
 770     // tolerate this.
 771     return false;
 772   }
 773   return true;
 774 }
 775 
 776 void AOTMetaspace::link_shared_classes(TRAPS) {
 777   AOTClassLinker::initialize();
 778   AOTClassInitializer::init_test_class(CHECK);
 779 




 780   while (true) {
 781     ResourceMark rm(THREAD);
 782     CollectClassesForLinking collect_classes;
 783     bool has_linked = false;
 784     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 785     for (int i = 0; i < mirrors->length(); i++) {
 786       OopHandle mirror = mirrors->at(i);
 787       InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
 788       if (may_be_eagerly_linked(ik)) {
 789         has_linked |= try_link_class(THREAD, ik);
 790       }



 791     }
 792 
 793     if (!has_linked) {
 794       break;
 795     }
 796     // Class linking includes verification which may load more classes.
 797     // Keep scanning until we have linked no more classes.
 798   }
 799 
 800   // Eargerly resolve all string constants in constant pools
 801   {
 802     ResourceMark rm(THREAD);
 803     CollectClassesForLinking collect_classes;
 804     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 805     for (int i = 0; i < mirrors->length(); i++) {
 806       OopHandle mirror = mirrors->at(i);
 807       InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
 808       AOTConstantPoolResolver::preresolve_string_cp_entries(ik, CHECK);



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

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


















 940 void AOTMetaspace::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) {







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

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




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














1035 
1036   if (AOTCodeCache::is_on_for_dump() && CDSConfig::is_dumping_final_static_archive()) {
1037     CDSConfig::enable_dumping_aot_code();
1038     {
1039       builder.start_ac_region();
1040       // Write the contents to AOT code region and close AOTCodeCache before packing the region
1041       AOTCodeCache::close();
1042       builder.end_ac_region();







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

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

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

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

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




1414   const char* static_archive = CDSConfig::input_static_archive_path();
1415   assert(static_archive != nullptr, "sanity");
1416   FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
1417   if (!mapinfo->open_as_input()) {
1418     delete(mapinfo);
1419     return nullptr;


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

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




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

   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::preload_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,

 723 
 724   log_info(cds)("Make training data shareable");
 725   _builder.make_training_data_shareable();
 726 
 727   // The vtable clones contain addresses of the current process.
 728   // We don't want to write these addresses into the archive.
 729   CppVtables::zero_archived_vtables();
 730 
 731   // Write the archive file
 732   if (CDSConfig::is_dumping_final_static_archive()) {
 733     FileMapInfo::free_current_info(); // FIXME: should not free current info
 734   }
 735   const char* static_archive = CDSConfig::output_archive_path();
 736   assert(static_archive != nullptr, "sanity");
 737   _map_info = new FileMapInfo(static_archive, true);
 738   _map_info->populate_header(AOTMetaspace::core_region_alignment());
 739   _map_info->set_early_serialized_data(early_serialized_data);
 740   _map_info->set_serialized_data(serialized_data);
 741   _map_info->set_cloned_vtables(CppVtables::vtables_serialized_base());
 742   _map_info->header()->set_class_location_config(cl_config);
 743 
 744   HeapShared::delete_tables_with_raw_oops();
 745 }
 746 
 747 class CollectClassesForLinking : public KlassClosure {
 748   GrowableArray<OopHandle> _mirrors;
 749 
 750 public:
 751    CollectClassesForLinking() : _mirrors() {
 752      // ClassLoaderDataGraph::loaded_classes_do_keepalive() requires ClassLoaderDataGraph_lock.
 753      // We cannot link the classes while holding this lock (or else we may run into deadlock).
 754      // Therefore, we need to first collect all the classes, keeping them alive by
 755      // holding onto their java_mirrors in global OopHandles. We then link the classes after
 756      // releasing the lock.
 757      MutexLocker lock(ClassLoaderDataGraph_lock);
 758      ClassLoaderDataGraph::loaded_classes_do_keepalive(this);
 759    }
 760 
 761   ~CollectClassesForLinking() {
 762     for (int i = 0; i < _mirrors.length(); i++) {
 763       _mirrors.at(i).release(Universe::vm_global());
 764     }
 765   }
 766 
 767   void do_cld(ClassLoaderData* cld) {
 768     assert(cld->is_alive(), "must be");
 769   }
 770 
 771   void do_klass(Klass* k) {
 772     if (k->is_instance_klass()) {
 773       _mirrors.append(OopHandle(Universe::vm_global(), k->java_mirror()));
 774     }
 775   }
 776 
 777   const GrowableArray<OopHandle>* mirrors() const { return &_mirrors; }
 778 };
 779 
 780 // Check if we can eagerly link this class at dump time, so we can avoid the
 781 // runtime linking overhead (especially verification)
 782 bool AOTMetaspace::may_be_eagerly_linked(InstanceKlass* ik) {
 783   if (CDSConfig::preserve_all_dumptime_verification_states(ik)) {
 784     assert(ik->can_be_verified_at_dumptime(), "sanity");
 785   }
 786   if (!ik->can_be_verified_at_dumptime()) {
 787     // For old classes, try to leave them in the unlinked state, so
 788     // we can still store them in the archive. They must be
 789     // linked/verified at runtime.
 790     return false;
 791   }
 792   if (CDSConfig::is_dumping_dynamic_archive() && ik->defined_by_other_loaders()) {
 793     // Linking of unregistered classes at this stage may cause more
 794     // classes to be resolved, resulting in calls to ClassLoader.loadClass()
 795     // that may not be expected by custom class loaders.
 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_shared_classes(TRAPS) {
 805   AOTClassLinker::initialize();
 806   AOTClassInitializer::init_test_class(CHECK);
 807 
 808   if (CDSConfig::is_dumping_final_static_archive()) {
 809     FinalImageRecipes::apply_recipes(CHECK);
 810   }
 811 
 812   while (true) {
 813     ResourceMark rm(THREAD);
 814     CollectClassesForLinking collect_classes;
 815     bool has_linked = false;
 816     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 817     for (int i = 0; i < mirrors->length(); i++) {
 818       OopHandle mirror = mirrors->at(i);
 819       InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
 820       if (may_be_eagerly_linked(ik)) {
 821         has_linked |= try_link_class(THREAD, ik);
 822       }
 823       if (CDSConfig::is_dumping_heap() && ik->is_linked() && !ik->is_initialized()) {
 824         AOTClassInitializer::maybe_preinit_class(ik, CHECK);
 825       }
 826     }
 827 
 828     if (!has_linked) {
 829       break;
 830     }
 831     // Class linking includes verification which may load more classes.
 832     // Keep scanning until we have linked no more classes.
 833   }
 834 
 835   // Eargerly resolve all string constants in constant pools
 836   {
 837     ResourceMark rm(THREAD);
 838     CollectClassesForLinking collect_classes;
 839     const GrowableArray<OopHandle>* mirrors = collect_classes.mirrors();
 840     for (int i = 0; i < mirrors->length(); i++) {
 841       OopHandle mirror = mirrors->at(i);
 842       InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror.resolve()));
 843       AOTConstantPoolResolver::preresolve_string_cp_entries(ik, CHECK);
 844       if (CDSConfig::is_dumping_preimage_static_archive()) {
 845         FinalImageRecipes::add_reflection_data_flags(ik, CHECK);
 846       }
 847     }
 848   }




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

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

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

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

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

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

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

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

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