1 /*
   2  * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "cds/aotLogging.hpp"
  26 #include "cds/aotMapLogger.hpp"
  27 #include "cds/aotMetaspace.hpp"
  28 #include "cds/cds_globals.hpp"
  29 #include "cds/cdsConfig.hpp"
  30 #include "cds/classListWriter.hpp"
  31 #include "cds/filemap.hpp"
  32 #include "cds/heapShared.inline.hpp"
  33 #include "classfile/classLoaderDataShared.hpp"
  34 #include "classfile/moduleEntry.hpp"
  35 #include "classfile/systemDictionaryShared.hpp"
  36 #include "code/aotCodeCache.hpp"
  37 #include "compiler/compilerDefinitions.inline.hpp"
  38 #include "include/jvm_io.h"
  39 #include "logging/log.hpp"
  40 #include "memory/universe.hpp"
  41 #include "prims/jvmtiAgentList.hpp"
  42 #include "prims/jvmtiExport.hpp"
  43 #include "runtime/arguments.hpp"
  44 #include "runtime/globals_extension.hpp"
  45 #include "runtime/java.hpp"
  46 #include "runtime/vmThread.hpp"
  47 #include "utilities/defaultStream.hpp"
  48 #include "utilities/formatBuffer.hpp"
  49 
  50 bool CDSConfig::_is_dumping_static_archive = false;
  51 bool CDSConfig::_is_dumping_preimage_static_archive = false;
  52 bool CDSConfig::_is_dumping_final_static_archive = false;
  53 bool CDSConfig::_is_dumping_dynamic_archive = false;
  54 bool CDSConfig::_is_using_optimized_module_handling = true;
  55 bool CDSConfig::_is_dumping_full_module_graph = true;
  56 bool CDSConfig::_is_using_full_module_graph = true;
  57 bool CDSConfig::_has_aot_linked_classes = false;
  58 bool CDSConfig::_is_single_command_training = false;
  59 bool CDSConfig::_has_temp_aot_config_file = false;
  60 bool CDSConfig::_old_cds_flags_used = false;
  61 bool CDSConfig::_new_aot_flags_used = false;
  62 bool CDSConfig::_disable_heap_dumping = false;
  63 bool CDSConfig::_is_at_aot_safepoint = false;
  64 
  65 const char* CDSConfig::_default_archive_path = nullptr;
  66 const char* CDSConfig::_input_static_archive_path = nullptr;
  67 const char* CDSConfig::_input_dynamic_archive_path = nullptr;
  68 const char* CDSConfig::_output_archive_path = nullptr;
  69 
  70 JavaThread* CDSConfig::_dumper_thread = nullptr;
  71 
  72 int CDSConfig::get_status() {
  73   assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized");
  74   return (is_dumping_aot_linked_classes()   ? IS_DUMPING_AOT_LINKED_CLASSES : 0) |
  75          (is_dumping_archive()              ? IS_DUMPING_ARCHIVE : 0) |
  76          (is_dumping_method_handles()       ? IS_DUMPING_METHOD_HANDLES : 0) |
  77          (is_dumping_static_archive()       ? IS_DUMPING_STATIC_ARCHIVE : 0) |
  78          (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) |
  79          (is_using_archive()                ? IS_USING_ARCHIVE : 0) |
  80          (is_dumping_heap()                 ? IS_DUMPING_HEAP : 0) |
  81          (is_logging_dynamic_proxies()      ? IS_LOGGING_DYNAMIC_PROXIES : 0);
  82 }
  83 
  84 DEBUG_ONLY(static bool _cds_ergo_initialize_started = false);
  85 
  86 void CDSConfig::ergo_initialize() {
  87   DEBUG_ONLY(_cds_ergo_initialize_started = true);
  88 
  89   if (is_dumping_static_archive() && !is_dumping_final_static_archive()) {
  90     // Note: -Xshare and -XX:AOTMode flags are mutually exclusive.
  91     // - Classic workflow: -Xshare:on and -Xshare:dump cannot take effect at the same time.
  92     // - JEP 483 workflow: -XX:AOTMode:record and -XX:AOTMode=on cannot take effect at the same time.
  93     // So we can never come to here with RequireSharedSpaces==true.
  94     assert(!RequireSharedSpaces, "sanity");
  95 
  96     // If dumping the classic archive, or making an AOT training run (dumping a preimage archive),
  97     // for sanity, parse all classes from classfiles.
  98     // TODO: in the future, if we want to support re-training on top of an existing AOT cache, this
  99     // needs to be changed.
 100     UseSharedSpaces = false;
 101   }
 102 
 103   // Initialize shared archive paths which could include both base and dynamic archive paths
 104   // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly.
 105   if (is_dumping_static_archive() || is_using_archive()) {
 106     if (new_aot_flags_used()) {
 107       ergo_init_aot_paths();
 108     } else {
 109       ergo_init_classic_archive_paths();
 110     }
 111   }
 112 
 113   if (!is_dumping_heap()) {
 114     _is_dumping_full_module_graph = false;
 115   }
 116 
 117   AOTMapLogger::ergo_initialize();
 118 
 119 #ifdef _LP64
 120   //
 121   // By default, when using AOTClassLinking, use the CompressedOops::HeapBasedNarrowOop
 122   // mode so that AOT code can be always work regardless of runtime heap range.
 123   //
 124   // If you are *absolutely sure* that the CompressedOops::mode() will be the same
 125   // between training and production runs (e.g., if you specify -Xmx128m for
 126   // both training and production runs, and you know the OS will always reserve
 127   // the heap under 4GB), you can explicitly disable this with:
 128   //     java -XX:+UnlockDiagnosticVMOptions -XX:-UseCompatibleCompressedOops ...
 129   // However, this is risky and there's a chance that the production run will be slower
 130   // than expected because it is unable to load the AOT code cache.
 131   //
 132   if (UseCompressedOops && AOTCodeCache::is_caching_enabled()) {
 133     FLAG_SET_ERGO_IF_DEFAULT(UseCompatibleCompressedOops, true);
 134   } else if (!FLAG_IS_DEFAULT(UseCompatibleCompressedOops)) {
 135     FLAG_SET_ERGO(UseCompatibleCompressedOops, false);
 136   }
 137 #endif // _LP64
 138 }
 139 
 140 const char* CDSConfig::default_archive_path() {
 141   // The path depends on UseCompressedOops, etc, which are set by GC ergonomics just
 142   // before CDSConfig::ergo_initialize() is called.
 143   assert(_cds_ergo_initialize_started, "sanity");
 144   if (_default_archive_path == nullptr) {
 145     stringStream tmp;
 146     if (is_vm_statically_linked()) {
 147       // It's easier to form the path using JAVA_HOME as os::jvm_path
 148       // gives the path to the launcher executable on static JDK.
 149       const char* subdir = WINDOWS_ONLY("bin") NOT_WINDOWS("lib");
 150       tmp.print("%s%s%s%s%s%sclasses",
 151                 Arguments::get_java_home(), os::file_separator(),
 152                 subdir, os::file_separator(),
 153                 Abstract_VM_Version::vm_variant(), os::file_separator());
 154     } else {
 155       // Assume .jsa is in the same directory where libjvm resides on
 156       // non-static JDK.
 157       char jvm_path[JVM_MAXPATHLEN];
 158       os::jvm_path(jvm_path, sizeof(jvm_path));
 159       char *end = strrchr(jvm_path, *os::file_separator());
 160       if (end != nullptr) *end = '\0';
 161       tmp.print("%s%sclasses", jvm_path, os::file_separator());
 162     }
 163 #ifdef _LP64
 164     if (!UseCompressedOops) {
 165       tmp.print_raw("_nocoops");
 166     }
 167     if (UseCompactObjectHeaders) {
 168       // Note that generation of xxx_coh.jsa variants require
 169       // --enable-cds-archive-coh at build time
 170       tmp.print_raw("_coh");
 171     }
 172 #endif
 173     tmp.print_raw(".jsa");
 174     _default_archive_path = os::strdup(tmp.base());
 175   }
 176   return _default_archive_path;
 177 }
 178 
 179 int CDSConfig::num_archive_paths(const char* path_spec) {
 180   if (path_spec == nullptr) {
 181     return 0;
 182   }
 183   int npaths = 1;
 184   char* p = (char*)path_spec;
 185   while (*p != '\0') {
 186     if (*p == os::path_separator()[0]) {
 187       npaths++;
 188     }
 189     p++;
 190   }
 191   return npaths;
 192 }
 193 
 194 void CDSConfig::extract_archive_paths(const char* archive_path,
 195                                       const char** base_archive_path,
 196                                       const char** top_archive_path) {
 197   char* begin_ptr = (char*)archive_path;
 198   char* end_ptr = strchr((char*)archive_path, os::path_separator()[0]);
 199   if (end_ptr == nullptr || end_ptr == begin_ptr) {
 200     vm_exit_during_initialization("Base archive was not specified", archive_path);
 201   }
 202   size_t len = end_ptr - begin_ptr;
 203   char* cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
 204   strncpy(cur_path, begin_ptr, len);
 205   cur_path[len] = '\0';
 206   *base_archive_path = cur_path;
 207 
 208   begin_ptr = ++end_ptr;
 209   if (*begin_ptr == '\0') {
 210     vm_exit_during_initialization("Top archive was not specified", archive_path);
 211   }
 212   end_ptr = strchr(begin_ptr, '\0');
 213   assert(end_ptr != nullptr, "sanity");
 214   len = end_ptr - begin_ptr;
 215   cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
 216   strncpy(cur_path, begin_ptr, len + 1);
 217   *top_archive_path = cur_path;
 218 }
 219 
 220 void CDSConfig::ergo_init_classic_archive_paths() {
 221   assert(_cds_ergo_initialize_started, "sanity");
 222   if (ArchiveClassesAtExit != nullptr) {
 223     assert(!RecordDynamicDumpInfo, "already checked");
 224     if (is_dumping_static_archive()) {
 225       vm_exit_during_initialization("-XX:ArchiveClassesAtExit cannot be used with -Xshare:dump");
 226     }
 227     check_unsupported_dumping_module_options();
 228 
 229     if (os::same_files(default_archive_path(), ArchiveClassesAtExit)) {
 230       vm_exit_during_initialization(
 231         "Cannot specify the default CDS archive for -XX:ArchiveClassesAtExit", default_archive_path());
 232     }
 233   }
 234 
 235   if (SharedArchiveFile == nullptr) {
 236     _input_static_archive_path = default_archive_path();
 237     if (is_dumping_static_archive()) {
 238       _output_archive_path = _input_static_archive_path;
 239     }
 240   } else {
 241     int num_archives = num_archive_paths(SharedArchiveFile);
 242     assert(num_archives > 0, "must be");
 243 
 244     if (is_dumping_archive() && num_archives > 1) {
 245       vm_exit_during_initialization(
 246         "Cannot have more than 1 archive file specified in -XX:SharedArchiveFile during CDS dumping");
 247     }
 248 
 249     if (is_dumping_static_archive()) {
 250       assert(num_archives == 1, "just checked above");
 251       // Static dump is simple: only one archive is allowed in SharedArchiveFile. This file
 252       // will be overwritten regardless of its contents
 253       _output_archive_path = SharedArchiveFile;
 254     } else {
 255       // SharedArchiveFile may specify one or two files. In case (c), the path for base.jsa
 256       // is read from top.jsa
 257       //    (a) 1 file:  -XX:SharedArchiveFile=base.jsa
 258       //    (b) 2 files: -XX:SharedArchiveFile=base.jsa:top.jsa
 259       //    (c) 2 files: -XX:SharedArchiveFile=top.jsa
 260       //
 261       // However, if either RecordDynamicDumpInfo or ArchiveClassesAtExit is used, we do not
 262       // allow cases (b) and (c). Case (b) is already checked above.
 263 
 264       if (num_archives > 2) {
 265         vm_exit_during_initialization(
 266           "Cannot have more than 2 archive files specified in the -XX:SharedArchiveFile option");
 267       }
 268 
 269       if (num_archives == 1) {
 270         const char* base_archive_path = nullptr;
 271         bool success =
 272           FileMapInfo::get_base_archive_name_from_header(SharedArchiveFile, &base_archive_path);
 273         if (!success) {
 274           // If +AutoCreateSharedArchive and the specified shared archive does not exist,
 275           // regenerate the dynamic archive base on default archive.
 276           if (AutoCreateSharedArchive && !os::file_exists(SharedArchiveFile)) {
 277             enable_dumping_dynamic_archive(SharedArchiveFile);
 278             FLAG_SET_ERGO(ArchiveClassesAtExit, SharedArchiveFile);
 279             _input_static_archive_path = default_archive_path();
 280             FLAG_SET_ERGO(SharedArchiveFile, nullptr);
 281          } else {
 282             if (AutoCreateSharedArchive) {
 283               warning("-XX:+AutoCreateSharedArchive is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info.");
 284               AutoCreateSharedArchive = false;
 285             }
 286             aot_log_error(aot)("Not a valid %s (%s)", type_of_archive_being_loaded(), SharedArchiveFile);
 287             Arguments::no_shared_spaces("invalid archive");
 288           }
 289         } else if (base_archive_path == nullptr) {
 290           // User has specified a single archive, which is a static archive.
 291           _input_static_archive_path = SharedArchiveFile;
 292         } else {
 293           // User has specified a single archive, which is a dynamic archive.
 294           _input_dynamic_archive_path = SharedArchiveFile;
 295           _input_static_archive_path = base_archive_path; // has been c-heap allocated.
 296         }
 297       } else {
 298         extract_archive_paths(SharedArchiveFile,
 299                               &_input_static_archive_path, &_input_dynamic_archive_path);
 300         if (_input_static_archive_path == nullptr) {
 301           assert(_input_dynamic_archive_path == nullptr, "must be");
 302           Arguments::no_shared_spaces("invalid archive");
 303         }
 304       }
 305 
 306       if (_input_dynamic_archive_path != nullptr) {
 307         // Check for case (c)
 308         if (RecordDynamicDumpInfo) {
 309           vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
 310                                         SharedArchiveFile);
 311         }
 312         if (ArchiveClassesAtExit != nullptr) {
 313           vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
 314                                         SharedArchiveFile);
 315         }
 316       }
 317 
 318       if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) {
 319           vm_exit_during_initialization(
 320             "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit",
 321             SharedArchiveFile);
 322       }
 323     }
 324   }
 325 }
 326 
 327 static char* bad_module_prop_key   = nullptr;
 328 static char* bad_module_prop_value = nullptr;
 329 
 330 void CDSConfig::check_internal_module_property(const char* key, const char* value) {
 331   if (Arguments::is_incompatible_cds_internal_module_property(key)) {
 332     stop_using_optimized_module_handling();
 333     if (bad_module_prop_key == nullptr) {
 334       // We don't want to print an unconditional warning here, as we are still processing the command line.
 335       // A later argument may specify something like -Xshare:off, which makes such a warning irrelevant.
 336       //
 337       // Instead, we save the info so we can warn when necessary: we are doing it only during AOT Cache
 338       // creation for now, but could add it to other places.
 339       bad_module_prop_key   = os::strdup(key);
 340       bad_module_prop_value = os::strdup(value);
 341     }
 342     aot_log_info(aot)("optimized module handling: disabled due to incompatible property: %s=%s", key, value);
 343   }
 344 }
 345 
 346 void CDSConfig::check_incompatible_property(const char* key, const char* value) {
 347   static const char* incompatible_properties[] = {
 348     "java.system.class.loader",
 349     "jdk.module.showModuleResolution",
 350     "jdk.module.validation"
 351   };
 352 
 353   for (const char* property : incompatible_properties) {
 354     if (strcmp(key, property) == 0) {
 355       stop_dumping_full_module_graph();
 356       stop_using_full_module_graph();
 357       aot_log_info(aot)("full module graph: disabled due to incompatible property: %s=%s", key, value);
 358       break;
 359     }
 360   }
 361 }
 362 
 363 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS.
 364 static const char* find_any_unsupported_module_option() {
 365   // Note that arguments.cpp has translated the command-line options into properties. If we find an
 366   // unsupported property, translate it back to its command-line option for better error reporting.
 367 
 368   // The following properties are checked by Arguments::is_internal_module_property() and cannot be
 369   // directly specified in the command-line.
 370   static const char* unsupported_module_properties[] = {
 371     "jdk.module.limitmods",
 372     "jdk.module.upgrade.path",
 373     "jdk.module.patch.0"
 374   };
 375   static const char* unsupported_module_options[] = {
 376     "--limit-modules",
 377     "--upgrade-module-path",
 378     "--patch-module"
 379   };
 380 
 381   assert(ARRAY_SIZE(unsupported_module_properties) == ARRAY_SIZE(unsupported_module_options), "must be");
 382   SystemProperty* sp = Arguments::system_properties();
 383   while (sp != nullptr) {
 384     for (uint i = 0; i < ARRAY_SIZE(unsupported_module_properties); i++) {
 385       if (strcmp(sp->key(), unsupported_module_properties[i]) == 0) {
 386         return unsupported_module_options[i];
 387       }
 388     }
 389     sp = sp->next();
 390   }
 391 
 392   return nullptr; // not found
 393 }
 394 
 395 void CDSConfig::check_unsupported_dumping_module_options() {
 396   assert(is_dumping_archive(), "this function is only used with CDS dump time");
 397   const char* option = find_any_unsupported_module_option();
 398   if (option != nullptr) {
 399     vm_exit_during_initialization("Cannot use the following option when dumping the shared archive", option);
 400   }
 401   // Check for an exploded module build in use with -Xshare:dump.
 402   if (!Arguments::has_jimage()) {
 403     vm_exit_during_initialization("Dumping the shared archive is not supported with an exploded module build");
 404   }
 405 }
 406 
 407 bool CDSConfig::has_unsupported_runtime_module_options() {
 408   assert(is_using_archive(), "this function is only used with -Xshare:{on,auto}");
 409   if (ArchiveClassesAtExit != nullptr) {
 410     // dynamic dumping, just return false for now.
 411     // check_unsupported_dumping_properties() will be called later to check the same set of
 412     // properties, and will exit the VM with the correct error message if the unsupported properties
 413     // are used.
 414     return false;
 415   }
 416   const char* option = find_any_unsupported_module_option();
 417   if (option != nullptr) {
 418     if (RequireSharedSpaces) {
 419       warning("CDS is disabled when the %s option is specified.", option);
 420     } else {
 421       if (new_aot_flags_used()) {
 422         aot_log_warning(aot)("AOT cache is disabled when the %s option is specified.", option);
 423       } else {
 424         aot_log_info(aot)("CDS is disabled when the %s option is specified.", option);
 425       }
 426     }
 427     return true;
 428   }
 429   return false;
 430 }
 431 
 432 #define CHECK_NEW_FLAG(f) check_new_flag(FLAG_IS_DEFAULT(f), #f)
 433 
 434 void CDSConfig::check_new_flag(bool new_flag_is_default, const char* new_flag_name) {
 435   if (old_cds_flags_used() && !new_flag_is_default) {
 436     vm_exit_during_initialization(err_msg("Option %s cannot be used at the same time with "
 437                                           "-Xshare:on, -Xshare:auto, -Xshare:off, -Xshare:dump, "
 438                                           "DumpLoadedClassList, SharedClassListFile, or SharedArchiveFile",
 439                                           new_flag_name));
 440   }
 441 }
 442 
 443 #define CHECK_SINGLE_PATH(f) check_flag_single_path(#f, f)
 444 
 445 void CDSConfig::check_flag_single_path(const char* flag_name, const char* value) {
 446   if (value != nullptr && num_archive_paths(value) != 1) {
 447     vm_exit_during_initialization(err_msg("Option %s must specify a single file name", flag_name));
 448   }
 449 }
 450 
 451 void CDSConfig::check_aot_flags() {
 452   if (!FLAG_IS_DEFAULT(DumpLoadedClassList) ||
 453       !FLAG_IS_DEFAULT(SharedClassListFile) ||
 454       !FLAG_IS_DEFAULT(SharedArchiveFile)) {
 455     _old_cds_flags_used = true;
 456   }
 457 
 458   // "New" AOT flags must not be mixed with "classic" CDS flags such as -Xshare:dump
 459   CHECK_NEW_FLAG(AOTCache);
 460   CHECK_NEW_FLAG(AOTCacheOutput);
 461   CHECK_NEW_FLAG(AOTConfiguration);
 462   CHECK_NEW_FLAG(AOTMode);
 463 
 464   CHECK_SINGLE_PATH(AOTCache);
 465   CHECK_SINGLE_PATH(AOTCacheOutput);
 466   CHECK_SINGLE_PATH(AOTConfiguration);
 467 
 468   if (FLAG_IS_DEFAULT(AOTCache) && AOTAdapterCaching) {
 469     log_debug(aot,codecache,init)("AOTCache is not specified - AOTAdapterCaching is ignored");
 470   }
 471   if (FLAG_IS_DEFAULT(AOTCache) && AOTStubCaching) {
 472     log_debug(aot,codecache,init)("AOTCache is not specified - AOTStubCaching is ignored");
 473   }
 474 
 475   bool has_cache = !FLAG_IS_DEFAULT(AOTCache);
 476   bool has_cache_output = !FLAG_IS_DEFAULT(AOTCacheOutput);
 477   bool has_config = !FLAG_IS_DEFAULT(AOTConfiguration);
 478   bool has_mode = !FLAG_IS_DEFAULT(AOTMode);
 479 
 480   if (!has_cache && !has_cache_output && !has_config && !has_mode) {
 481     // AOT flags are not used. Use classic CDS workflow
 482     return;
 483   }
 484 
 485   if (has_cache && has_cache_output) {
 486     vm_exit_during_initialization("Only one of AOTCache or AOTCacheOutput can be specified");
 487   }
 488 
 489   if (!has_cache && (!has_mode || strcmp(AOTMode, "auto") == 0)) {
 490     if (has_cache_output) {
 491       // If AOTCacheOutput has been set, effective mode is "record".
 492       // Default value for AOTConfiguration, if necessary, will be assigned in check_aotmode_record().
 493       log_info(aot)("Selected AOTMode=record because AOTCacheOutput is specified");
 494       FLAG_SET_ERGO(AOTMode, "record");
 495     }
 496   }
 497 
 498   // At least one AOT flag has been used
 499   _new_aot_flags_used = true;
 500 
 501   if (FLAG_IS_DEFAULT(AOTMode) || strcmp(AOTMode, "auto") == 0 || strcmp(AOTMode, "on") == 0) {
 502     check_aotmode_auto_or_on();
 503   } else if (strcmp(AOTMode, "off") == 0) {
 504     check_aotmode_off();
 505   } else if (strcmp(AOTMode, "record") == 0) {
 506     check_aotmode_record();
 507   } else {
 508     assert(strcmp(AOTMode, "create") == 0, "checked by AOTModeConstraintFunc");
 509     check_aotmode_create();
 510   }
 511 }
 512 
 513 void CDSConfig::check_aotmode_off() {
 514   UseSharedSpaces = false;
 515   RequireSharedSpaces = false;
 516 }
 517 
 518 void CDSConfig::check_aotmode_auto_or_on() {
 519   if (!FLAG_IS_DEFAULT(AOTConfiguration)) {
 520     vm_exit_during_initialization(err_msg("AOTConfiguration can only be used with when AOTMode is record or create (selected AOTMode = %s)",
 521                                           FLAG_IS_DEFAULT(AOTMode) ? "auto" : AOTMode));
 522   }
 523 
 524   UseSharedSpaces = true;
 525   if (FLAG_IS_DEFAULT(AOTMode) || (strcmp(AOTMode, "auto") == 0)) {
 526     RequireSharedSpaces = false;
 527   } else {
 528     assert(strcmp(AOTMode, "on") == 0, "already checked");
 529     RequireSharedSpaces = true;
 530   }
 531 }
 532 
 533 // %p substitution in AOTCache, AOTCacheOutput and AOTCacheConfiguration
 534 static void substitute_aot_filename(JVMFlagsEnum flag_enum) {
 535   JVMFlag* flag = JVMFlag::flag_from_enum(flag_enum);
 536   const char* filename = flag->read<const char*>();
 537   assert(filename != nullptr, "must not have default value");
 538 
 539   // For simplicity, we don't allow %p/%t to be specified twice, because make_log_name()
 540   // substitutes only the first occurrence. Otherwise, if we run with
 541   //     java -XX:AOTCacheOutput=%p%p.aot
 542  // it will end up with both the pid of the training process and the assembly process.
 543   const char* first_p = strstr(filename, "%p");
 544   if (first_p != nullptr && strstr(first_p + 2, "%p") != nullptr) {
 545     vm_exit_during_initialization(err_msg("%s cannot contain more than one %%p", flag->name()));
 546   }
 547   const char* first_t = strstr(filename, "%t");
 548   if (first_t != nullptr && strstr(first_t + 2, "%t") != nullptr) {
 549     vm_exit_during_initialization(err_msg("%s cannot contain more than one %%t", flag->name()));
 550   }
 551 
 552   // Note: with single-command training, %p will be the pid of the training process, not the
 553   // assembly process.
 554   const char* new_filename = make_log_name(filename, nullptr);
 555   if (strcmp(filename, new_filename) != 0) {
 556     JVMFlag::Error err = JVMFlagAccess::set_ccstr(flag, &new_filename, JVMFlagOrigin::ERGONOMIC);
 557     assert(err == JVMFlag::SUCCESS, "must never fail");
 558   }
 559   FREE_C_HEAP_ARRAY(char, new_filename);
 560 }
 561 
 562 void CDSConfig::check_aotmode_record() {
 563   bool has_config = !FLAG_IS_DEFAULT(AOTConfiguration);
 564   bool has_output = !FLAG_IS_DEFAULT(AOTCacheOutput);
 565 
 566   if (!has_output && !has_config) {
 567       vm_exit_during_initialization("At least one of AOTCacheOutput and AOTConfiguration must be specified when using -XX:AOTMode=record");
 568   }
 569 
 570   if (has_output) {
 571     _is_single_command_training = true;
 572     substitute_aot_filename(FLAG_MEMBER_ENUM(AOTCacheOutput));
 573     if (!has_config) {
 574       // Too early; can't use resource allocation yet.
 575       size_t len = strlen(AOTCacheOutput) + 10;
 576       char* temp = AllocateHeap(len, mtArguments);
 577       jio_snprintf(temp, len, "%s.config", AOTCacheOutput);
 578       FLAG_SET_ERGO(AOTConfiguration, temp);
 579       FreeHeap(temp);
 580       _has_temp_aot_config_file = true;
 581     }
 582   }
 583 
 584   if (!FLAG_IS_DEFAULT(AOTCache)) {
 585     vm_exit_during_initialization("AOTCache must not be specified when using -XX:AOTMode=record");
 586   }
 587 
 588   substitute_aot_filename(FLAG_MEMBER_ENUM(AOTConfiguration));
 589 
 590   UseSharedSpaces = false;
 591   RequireSharedSpaces = false;
 592   _is_dumping_static_archive = true;
 593   _is_dumping_preimage_static_archive = true;
 594 
 595   // At VM exit, the module graph may be contaminated with program states.
 596   // We will rebuild the module graph when dumping the CDS final image.
 597   disable_heap_dumping();
 598 }
 599 
 600 void CDSConfig::check_aotmode_create() {
 601   if (FLAG_IS_DEFAULT(AOTConfiguration)) {
 602     vm_exit_during_initialization("AOTConfiguration must be specified when using -XX:AOTMode=create");
 603   }
 604 
 605   bool has_cache = !FLAG_IS_DEFAULT(AOTCache);
 606   bool has_cache_output = !FLAG_IS_DEFAULT(AOTCacheOutput);
 607 
 608   assert(!(has_cache && has_cache_output), "already checked");
 609 
 610   if (!has_cache && !has_cache_output) {
 611     vm_exit_during_initialization("AOTCache or AOTCacheOutput must be specified when using -XX:AOTMode=create");
 612   }
 613 
 614   if (!has_cache) {
 615     precond(has_cache_output);
 616     FLAG_SET_ERGO(AOTCache, AOTCacheOutput);
 617   }
 618   // No need to check for (!has_cache_output), as we don't look at AOTCacheOutput after here.
 619 
 620   substitute_aot_filename(FLAG_MEMBER_ENUM(AOTCache));
 621 
 622   _is_dumping_final_static_archive = true;
 623   UseSharedSpaces = true;
 624   RequireSharedSpaces = true;
 625 
 626   if (!FileMapInfo::is_preimage_static_archive(AOTConfiguration)) {
 627     vm_exit_during_initialization("Must be a valid AOT configuration generated by the current JVM", AOTConfiguration);
 628   }
 629 
 630   CDSConfig::enable_dumping_static_archive();
 631 
 632   // We don't load any agents in the assembly phase, so we can ensure that the agents
 633   // cannot affect the contents of the AOT cache. E.g., we don't want the agents to
 634   // redefine any cached classes. We also don't want the agents to modify heap objects that
 635   // are cached.
 636   //
 637   // Since application is not executed in the assembly phase, there's no need to load
 638   // the agents anyway -- no one will notice that the agents are not loaded.
 639   log_info(aot)("Disabled all JVMTI agents during -XX:AOTMode=create");
 640   JvmtiAgentList::disable_agent_list();
 641 }
 642 
 643 void CDSConfig::ergo_init_aot_paths() {
 644   assert(_cds_ergo_initialize_started, "sanity");
 645   if (is_dumping_static_archive()) {
 646     if (is_dumping_preimage_static_archive()) {
 647       _output_archive_path = AOTConfiguration;
 648     } else {
 649       assert(is_dumping_final_static_archive(), "must be");
 650       _input_static_archive_path = AOTConfiguration;
 651       _output_archive_path = AOTCache;
 652     }
 653   } else if (is_using_archive()) {
 654     if (FLAG_IS_DEFAULT(AOTCache)) {
 655       // Only -XX:AOTMode={auto,on} is specified
 656       _input_static_archive_path = default_archive_path();
 657     } else {
 658       _input_static_archive_path = AOTCache;
 659     }
 660   }
 661 }
 662 
 663 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line) {
 664   assert(!_cds_ergo_initialize_started, "This is called earlier than CDSConfig::ergo_initialize()");
 665 
 666   check_aot_flags();
 667 
 668   if (!FLAG_IS_DEFAULT(AOTMode)) {
 669     // Using any form of the new AOTMode switch enables enhanced optimizations.
 670     FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true);
 671   }
 672 
 673   setup_compiler_args();
 674 
 675   if (AOTClassLinking) {
 676     // If AOTClassLinking is specified, enable all these optimizations by default.
 677     FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true);
 678     FLAG_SET_ERGO_IF_DEFAULT(ArchiveDynamicProxies, true);
 679     FLAG_SET_ERGO_IF_DEFAULT(ArchiveLoaderLookupCache, true);
 680     FLAG_SET_ERGO_IF_DEFAULT(ArchiveReflectionData, true);
 681 
 682     // For simplicity, enable these by default only for new workflow
 683     if (new_aot_flags_used()) {
 684       // These flags will be removed when JDK-8350550 is merged from mainline
 685       FLAG_SET_ERGO_IF_DEFAULT(ArchivePackages, true);
 686       FLAG_SET_ERGO_IF_DEFAULT(ArchiveProtectionDomains, true);
 687     }
 688   } else {
 689     // All of these *might* depend on AOTClassLinking. Better be safe than sorry.
 690     FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);
 691     FLAG_SET_ERGO(ArchiveDynamicProxies, false);
 692     FLAG_SET_ERGO(ArchiveLoaderLookupCache, false);
 693     FLAG_SET_ERGO(ArchivePackages, false);
 694     FLAG_SET_ERGO(ArchiveProtectionDomains, false);
 695     FLAG_SET_ERGO(ArchiveReflectionData, false);
 696 
 697     if (CDSConfig::is_dumping_archive()) {
 698       FLAG_SET_ERGO(AOTRecordTraining, false);
 699       FLAG_SET_ERGO(AOTReplayTraining, false);
 700       AOTCodeCache::disable_caching();
 701     }
 702   }
 703   if (is_dumping_static_archive()) {
 704     if (is_dumping_preimage_static_archive()) {
 705       // Don't tweak execution mode during AOT training run
 706     } else if (is_dumping_final_static_archive()) {
 707       if (Arguments::mode() == Arguments::_comp) {
 708         // AOT assembly phase submits the non-blocking compilation requests
 709         // for methods collected during training run, then waits for all compilations
 710         // to complete. With -Xcomp, we block for each compilation request, which is
 711         // counter-productive. Switching back to mixed mode improves testing time
 712         // with AOT and -Xcomp.
 713         Arguments::set_mode_flags(Arguments::_mixed);
 714       }
 715     } else if (!mode_flag_cmd_line) {
 716       // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
 717       //
 718       // If your classlist is large and you don't care about deterministic dumping, you can use
 719       // -Xshare:dump -Xmixed to improve dumping speed.
 720       Arguments::set_mode_flags(Arguments::_int);
 721     } else if (Arguments::mode() == Arguments::_comp) {
 722       // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
 723       // Java code, so there's not much benefit in running -Xcomp.
 724       aot_log_info(aot)("reduced -Xcomp to -Xmixed for static dumping");
 725       Arguments::set_mode_flags(Arguments::_mixed);
 726     }
 727 
 728     // String deduplication may cause CDS to iterate the strings in different order from one
 729     // run to another which resulting in non-determinstic CDS archives.
 730     // Disable UseStringDeduplication while dumping CDS archive.
 731     UseStringDeduplication = false;
 732   }
 733 
 734   // RecordDynamicDumpInfo is not compatible with ArchiveClassesAtExit
 735   if (ArchiveClassesAtExit != nullptr && RecordDynamicDumpInfo) {
 736     jio_fprintf(defaultStream::output_stream(),
 737                 "-XX:+RecordDynamicDumpInfo cannot be used with -XX:ArchiveClassesAtExit.\n");
 738     return false;
 739   }
 740 
 741   if (ArchiveClassesAtExit == nullptr && !RecordDynamicDumpInfo) {
 742     disable_dumping_dynamic_archive();
 743   } else {
 744     enable_dumping_dynamic_archive(ArchiveClassesAtExit);
 745   }
 746 
 747   if (AutoCreateSharedArchive) {
 748     if (SharedArchiveFile == nullptr) {
 749       aot_log_warning(aot)("-XX:+AutoCreateSharedArchive requires -XX:SharedArchiveFile");
 750       return false;
 751     }
 752     if (ArchiveClassesAtExit != nullptr) {
 753       aot_log_warning(aot)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit");
 754       return false;
 755     }
 756   }
 757 
 758   if (is_using_archive() && patch_mod_javabase) {
 759     Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
 760   }
 761   if (is_using_archive() && has_unsupported_runtime_module_options()) {
 762     UseSharedSpaces = false;
 763   }
 764 
 765   if (is_dumping_archive()) {
 766     // Always verify non-system classes during CDS dump
 767     if (!BytecodeVerificationRemote) {
 768       BytecodeVerificationRemote = true;
 769       aot_log_info(aot)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
 770     }
 771   }
 772 
 773   if (AOTClassLinking) {
 774     if (is_dumping_final_static_archive() && !is_dumping_full_module_graph()) {
 775       if (bad_module_prop_key != nullptr) {
 776         log_warning(cds)("optimized module handling/full module graph: disabled due to incompatible property: %s=%s",
 777                          bad_module_prop_key, bad_module_prop_value);
 778       }
 779       vm_exit_during_initialization("AOT cache cannot be created because AOTClassLinking is enabled but full module graph is disabled");
 780     }
 781   }
 782 
 783   return true;
 784 }
 785 
 786 void CDSConfig::setup_compiler_args() {
 787   // AOT profiles and AOT-compiled methods are supported only in the JEP 483 workflow.
 788   bool can_dump_profile_and_compiled_code = !CompilerConfig::is_interpreter_only() && AOTClassLinking && new_aot_flags_used();
 789   bool can_use_profile_and_compiled_code = !CompilerConfig::is_interpreter_only() && new_aot_flags_used();
 790 
 791   if (is_dumping_preimage_static_archive() && can_dump_profile_and_compiled_code) {
 792     // JEP 483 workflow -- training
 793     FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
 794     FLAG_SET_ERGO(AOTReplayTraining, false);
 795     AOTCodeCache::disable_caching(); // No AOT code generation during training run
 796     FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
 797   } else if (is_dumping_final_static_archive() && can_dump_profile_and_compiled_code) {
 798     // JEP 483 workflow -- assembly
 799     FLAG_SET_ERGO(AOTRecordTraining, false);
 800     FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
 801     AOTCodeCache::enable_caching(); // Generate AOT code during assembly phase.
 802     FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
 803     disable_dumping_aot_code();     // Don't dump AOT code until metadata and heap are dumped.
 804   } else if (is_using_archive() && can_use_profile_and_compiled_code) {
 805     // JEP 483 workflow -- production
 806     FLAG_SET_ERGO(AOTRecordTraining, false);
 807     FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
 808     AOTCodeCache::enable_caching();
 809     FLAG_SET_ERGO_IF_DEFAULT(UseAOTCodeLoadThread, true);
 810   } else {
 811     FLAG_SET_ERGO(AOTReplayTraining, false);
 812     FLAG_SET_ERGO(AOTRecordTraining, false);
 813     AOTCodeCache::disable_caching();
 814     FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
 815   }
 816 }
 817 
 818 void CDSConfig::prepare_for_dumping() {
 819   assert(CDSConfig::is_dumping_archive(), "sanity");
 820 
 821   if (is_dumping_dynamic_archive() && AOTClassLinking) {
 822     if (FLAG_IS_CMDLINE(AOTClassLinking)) {
 823       log_warning(cds)("AOTClassLinking is not supported for dynamic CDS archive");
 824     }
 825     FLAG_SET_ERGO(AOTClassLinking, false);
 826   }
 827 
 828   if (is_dumping_dynamic_archive() && !is_using_archive()) {
 829     assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives");
 830 
 831     // This could happen if SharedArchiveFile has failed to load:
 832     // - -Xshare:off was specified
 833     // - SharedArchiveFile points to an non-existent file.
 834     // - SharedArchiveFile points to an archive that has failed CRC check
 835     // - SharedArchiveFile is not specified and the VM doesn't have a compatible default archive
 836 
 837 #define __THEMSG " is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."
 838     if (RecordDynamicDumpInfo) {
 839       aot_log_error(aot)("-XX:+RecordDynamicDumpInfo%s", __THEMSG);
 840       AOTMetaspace::unrecoverable_loading_error();
 841     } else {
 842       assert(ArchiveClassesAtExit != nullptr, "sanity");
 843       aot_log_warning(aot)("-XX:ArchiveClassesAtExit" __THEMSG);
 844     }
 845 #undef __THEMSG
 846     disable_dumping_dynamic_archive();
 847     return;
 848   }
 849 
 850   check_unsupported_dumping_module_options();
 851 }
 852 
 853 bool CDSConfig::is_dumping_classic_static_archive() {
 854   return _is_dumping_static_archive &&
 855     !is_dumping_preimage_static_archive() &&
 856     !is_dumping_final_static_archive();
 857 }
 858 
 859 bool CDSConfig::is_dumping_preimage_static_archive() {
 860   return _is_dumping_preimage_static_archive;
 861 }
 862 
 863 bool CDSConfig::is_dumping_preimage_static_archive_with_triggers() {
 864   return (!FLAG_IS_DEFAULT(AOTEndTrainingOnMethodEntry)) && is_dumping_preimage_static_archive();
 865 }
 866 
 867 bool CDSConfig::is_dumping_final_static_archive() {
 868   return _is_dumping_final_static_archive;
 869 }
 870 
 871 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) {
 872   _is_dumping_dynamic_archive = true;
 873   if (output_path == nullptr) {
 874     // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo
 875     // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual
 876     // output path.
 877     _output_archive_path = nullptr;
 878   } else {
 879     _output_archive_path = os::strdup_check_oom(output_path, mtArguments);
 880   }
 881 }
 882 
 883 bool CDSConfig::allow_only_single_java_thread() {
 884   // See comments in JVM_StartThread()
 885   return is_dumping_classic_static_archive() || is_dumping_final_static_archive();
 886 }
 887 
 888 bool CDSConfig::is_logging_dynamic_proxies() {
 889   return ClassListWriter::is_enabled() || is_dumping_preimage_static_archive();
 890 }
 891 
 892 bool CDSConfig::is_using_archive() {
 893   return UseSharedSpaces;
 894 }
 895 
 896 bool CDSConfig::is_using_only_default_archive() {
 897   return is_using_archive() &&
 898          input_static_archive_path() != nullptr &&
 899          default_archive_path() != nullptr &&
 900          strcmp(input_static_archive_path(), default_archive_path()) == 0 &&
 901          input_dynamic_archive_path() == nullptr;
 902 }
 903 
 904 bool CDSConfig::is_logging_lambda_form_invokers() {
 905   return ClassListWriter::is_enabled() || is_dumping_dynamic_archive() || is_dumping_preimage_static_archive();
 906 }
 907 
 908 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
 909   if (is_dumping_final_static_archive()) {
 910     // No need to regenerate -- the lambda form invokers should have been regenerated
 911     // in the preimage archive (if allowed)
 912     return false;
 913   } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) {
 914     // The base archive has aot-linked classes that may have AOT-resolved CP references
 915     // that point to the lambda form invokers in the base archive. Such pointers will
 916     // be invalid if lambda form invokers are regenerated in the dynamic archive.
 917     return false;
 918   } else {
 919     return is_dumping_archive();
 920   }
 921 }
 922 
 923 void CDSConfig::stop_using_optimized_module_handling() {
 924   _is_using_optimized_module_handling = false;
 925   _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling()
 926   _is_using_full_module_graph = false; // This requires is_using_optimized_module_handling()
 927 }
 928 
 929 
 930 CDSConfig::DumperThreadMark::DumperThreadMark(JavaThread* current) {
 931   assert(_dumper_thread == nullptr, "sanity");
 932   _dumper_thread = current;
 933 }
 934 
 935 CDSConfig::DumperThreadMark::~DumperThreadMark() {
 936   assert(_dumper_thread != nullptr, "sanity");
 937   _dumper_thread = nullptr;
 938 }
 939 
 940 bool CDSConfig::current_thread_is_vm_or_dumper() {
 941   Thread* t = Thread::current();
 942   return t->is_VM_thread() || t == _dumper_thread;
 943 }
 944 
 945 bool CDSConfig::current_thread_is_dumper() {
 946   return Thread::current() == _dumper_thread;
 947 }
 948 
 949 const char* CDSConfig::type_of_archive_being_loaded() {
 950   if (is_dumping_final_static_archive()) {
 951     return "AOT configuration file";
 952   } else if (new_aot_flags_used()) {
 953     return "AOT cache";
 954   } else {
 955     return "shared archive file";
 956   }
 957 }
 958 
 959 const char* CDSConfig::type_of_archive_being_written() {
 960   if (is_dumping_preimage_static_archive()) {
 961     return "AOT configuration file";
 962   } else if (new_aot_flags_used()) {
 963     return "AOT cache";
 964   } else {
 965     return "shared archive file";
 966   }
 967 }
 968 
 969 // If an incompatible VM options is found, return a text message that explains why
 970 static const char* check_options_incompatible_with_dumping_heap() {
 971 #if INCLUDE_CDS_JAVA_HEAP
 972   if (!UseCompressedClassPointers) {
 973     return "UseCompressedClassPointers must be true";
 974   }
 975 
 976   return nullptr;
 977 #else
 978   return "JVM not configured for writing Java heap objects";
 979 #endif
 980 }
 981 
 982 void CDSConfig::log_reasons_for_not_dumping_heap() {
 983   const char* reason;
 984 
 985   assert(!is_dumping_heap(), "sanity");
 986 
 987   if (_disable_heap_dumping) {
 988     reason = "Programmatically disabled";
 989   } else {
 990     reason = check_options_incompatible_with_dumping_heap();
 991   }
 992 
 993   assert(reason != nullptr, "sanity");
 994   aot_log_info(aot)("Archived java heap is not supported: %s", reason);
 995 }
 996 
 997 // This is *Legacy* optimization for lambdas before JEP 483. May be removed in the future.
 998 bool CDSConfig::is_dumping_lambdas_in_legacy_mode() {
 999   return !is_dumping_method_handles();
1000 }
1001 
1002 bool CDSConfig::is_preserving_verification_constraints() {
1003   // Verification dependencies are classes used in assignability checks by the
1004   // bytecode verifier. In the following example, the verification dependencies
1005   // for X are A and B.
1006   //
1007   //     class X {
1008   //        A getA() { return new B(); }
1009   //     }
1010   //
1011   // With the AOT cache, we can ensure that all the verification dependencies
1012   // (A and B in the above example) are unconditionally loaded during the bootstrap
1013   // of the production run. This means that if a class was successfully verified
1014   // in the assembly phase, all of the verifier's assignability checks will remain
1015   // valid in the production run, so we don't need to verify aot-linked classes again.
1016 
1017   if (is_dumping_preimage_static_archive()) { // writing AOT config
1018     return AOTClassLinking;
1019   } else if (is_dumping_final_static_archive()) { // writing AOT cache
1020     return is_dumping_aot_linked_classes();
1021   } else if (is_dumping_classic_static_archive()) {
1022     return is_dumping_aot_linked_classes();
1023   } else {
1024     return false;
1025   }
1026 }
1027 
1028 bool CDSConfig::is_old_class_for_verifier(const InstanceKlass* ik) {
1029   return ik->major_version() < 50 /*JAVA_6_VERSION*/;
1030 }
1031 
1032 #if INCLUDE_CDS_JAVA_HEAP
1033 bool CDSConfig::are_vm_options_incompatible_with_dumping_heap() {
1034   return check_options_incompatible_with_dumping_heap() != nullptr;
1035 }
1036 
1037 bool CDSConfig::is_dumping_heap() {
1038   if (!(is_dumping_classic_static_archive() || is_dumping_final_static_archive())
1039       || are_vm_options_incompatible_with_dumping_heap()
1040       || _disable_heap_dumping) {
1041     return false;
1042   }
1043   return true;
1044 }
1045 
1046 bool CDSConfig::is_loading_heap() {
1047   return HeapShared::is_archived_heap_in_use();
1048 }
1049 
1050 bool CDSConfig::is_using_full_module_graph() {
1051   if (ClassLoaderDataShared::is_full_module_graph_loaded()) {
1052     return true;
1053   }
1054 
1055   if (!_is_using_full_module_graph) {
1056     return false;
1057   }
1058 
1059   if (is_using_archive() && HeapShared::can_use_archived_heap()) {
1060     // Classes used by the archived full module graph are loaded in JVMTI early phase.
1061     assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()),
1062            "CDS should be disabled if early class hooks are enabled");
1063     return true;
1064   } else {
1065     _is_using_full_module_graph = false;
1066     return false;
1067   }
1068 }
1069 
1070 void CDSConfig::stop_dumping_full_module_graph(const char* reason) {
1071   if (_is_dumping_full_module_graph) {
1072     _is_dumping_full_module_graph = false;
1073     if (reason != nullptr) {
1074       aot_log_info(aot)("full module graph cannot be dumped: %s", reason);
1075     }
1076   }
1077 }
1078 
1079 void CDSConfig::stop_using_full_module_graph(const char* reason) {
1080   assert(!ClassLoaderDataShared::is_full_module_graph_loaded(), "you call this function too late!");
1081   if (_is_using_full_module_graph) {
1082     _is_using_full_module_graph = false;
1083     if (reason != nullptr) {
1084       aot_log_info(aot)("full module graph cannot be loaded: %s", reason);
1085     }
1086   }
1087 }
1088 
1089 bool CDSConfig::is_dumping_aot_linked_classes() {
1090   if (is_dumping_classic_static_archive() || is_dumping_final_static_archive()) {
1091     // FMG is required to guarantee that all cached boot/platform/app classes
1092     // are visible in the production run, so they can be unconditionally
1093     // loaded during VM bootstrap.
1094     return is_dumping_full_module_graph() && AOTClassLinking;
1095   } else {
1096     return false;
1097   }
1098 }
1099 
1100 bool CDSConfig::is_using_aot_linked_classes() {
1101   // Make sure we have the exact same module graph as in the assembly phase, or else
1102   // some aot-linked classes may not be visible so cannot be loaded.
1103   return is_using_full_module_graph() && _has_aot_linked_classes;
1104 }
1105 
1106 bool CDSConfig::is_dumping_dynamic_proxies() {
1107   return is_dumping_full_module_graph() && is_dumping_invokedynamic() && ArchiveDynamicProxies;
1108 }
1109 
1110 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) {
1111   _has_aot_linked_classes |= has_aot_linked_classes;
1112 }
1113 
1114 bool CDSConfig::is_initing_classes_at_dump_time() {
1115   return is_dumping_heap() && is_dumping_aot_linked_classes();
1116 }
1117 
1118 bool CDSConfig::is_dumping_invokedynamic() {
1119   // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap
1120   // objects used by the archive indy callsites may be replaced at runtime.
1121   return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap();
1122 }
1123 
1124 bool CDSConfig::is_dumping_reflection_data() {
1125   // reflection data use LambdaForm classes
1126   return ArchiveReflectionData && is_dumping_invokedynamic();
1127 }
1128 
1129 // When we are dumping aot-linked classes and we are able to write archived heap objects, we automatically
1130 // enable the archiving of MethodHandles. This will in turn enable the archiving of MethodTypes and hidden
1131 // classes that are used in the implementation of MethodHandles.
1132 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
1133 // and dynamic proxies.
1134 bool CDSConfig::is_dumping_method_handles() {
1135   return is_initing_classes_at_dump_time();
1136 }
1137 
1138 #endif // INCLUDE_CDS_JAVA_HEAP
1139 
1140 // AOT code generation and its archiving is disabled by default.
1141 // We enable it only in the final image dump after the metadata and heap are dumped.
1142 // This affects only JITed code because it may have embedded oops and metadata pointers
1143 // which AOT code encodes as offsets in final CDS archive regions.
1144 
1145 static bool _is_dumping_aot_code = false;
1146 
1147 bool CDSConfig::is_dumping_aot_code() {
1148   return _is_dumping_aot_code;
1149 }
1150 
1151 void CDSConfig::disable_dumping_aot_code() {
1152   _is_dumping_aot_code = false;
1153 }
1154 
1155 void CDSConfig::enable_dumping_aot_code() {
1156   _is_dumping_aot_code = true;
1157 }
1158 
1159 bool CDSConfig::is_dumping_adapters() {
1160   return (AOTAdapterCaching && is_dumping_final_static_archive());
1161 }