1 /*
   2  * Copyright (c) 2023, 2026, 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   _is_using_optimized_module_handling = false;
 598   _is_using_full_module_graph = false;
 599   _is_dumping_full_module_graph = false;
 600 }
 601 
 602 void CDSConfig::check_aotmode_create() {
 603   if (FLAG_IS_DEFAULT(AOTConfiguration)) {
 604     vm_exit_during_initialization("AOTConfiguration must be specified when using -XX:AOTMode=create");
 605   }
 606 
 607   bool has_cache = !FLAG_IS_DEFAULT(AOTCache);
 608   bool has_cache_output = !FLAG_IS_DEFAULT(AOTCacheOutput);
 609 
 610   assert(!(has_cache && has_cache_output), "already checked");
 611 
 612   if (!has_cache && !has_cache_output) {
 613     vm_exit_during_initialization("AOTCache or AOTCacheOutput must be specified when using -XX:AOTMode=create");
 614   }
 615 
 616   if (!has_cache) {
 617     precond(has_cache_output);
 618     FLAG_SET_ERGO(AOTCache, AOTCacheOutput);
 619   }
 620   // No need to check for (!has_cache_output), as we don't look at AOTCacheOutput after here.
 621 
 622   substitute_aot_filename(FLAG_MEMBER_ENUM(AOTCache));
 623 
 624   _is_dumping_final_static_archive = true;
 625   _is_using_full_module_graph = false;
 626   UseSharedSpaces = true;
 627   RequireSharedSpaces = true;
 628 
 629   if (!FileMapInfo::is_preimage_static_archive(AOTConfiguration)) {
 630     vm_exit_during_initialization("Must be a valid AOT configuration generated by the current JVM", AOTConfiguration);
 631   }
 632 
 633   CDSConfig::enable_dumping_static_archive();
 634 
 635   // We don't load any agents in the assembly phase, so we can ensure that the agents
 636   // cannot affect the contents of the AOT cache. E.g., we don't want the agents to
 637   // redefine any cached classes. We also don't want the agents to modify heap objects that
 638   // are cached.
 639   //
 640   // Since application is not executed in the assembly phase, there's no need to load
 641   // the agents anyway -- no one will notice that the agents are not loaded.
 642   log_info(aot)("Disabled all JVMTI agents during -XX:AOTMode=create");
 643   JvmtiAgentList::disable_agent_list();
 644 }
 645 
 646 void CDSConfig::ergo_init_aot_paths() {
 647   assert(_cds_ergo_initialize_started, "sanity");
 648   if (is_dumping_static_archive()) {
 649     if (is_dumping_preimage_static_archive()) {
 650       _output_archive_path = AOTConfiguration;
 651     } else {
 652       assert(is_dumping_final_static_archive(), "must be");
 653       _input_static_archive_path = AOTConfiguration;
 654       _output_archive_path = AOTCache;
 655     }
 656   } else if (is_using_archive()) {
 657     if (FLAG_IS_DEFAULT(AOTCache)) {
 658       // Only -XX:AOTMode={auto,on} is specified
 659       _input_static_archive_path = default_archive_path();
 660     } else {
 661       _input_static_archive_path = AOTCache;
 662     }
 663   }
 664 }
 665 
 666 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line) {
 667   assert(!_cds_ergo_initialize_started, "This is called earlier than CDSConfig::ergo_initialize()");
 668 
 669   check_aot_flags();
 670 
 671   if (!FLAG_IS_DEFAULT(AOTMode)) {
 672     // Using any form of the new AOTMode switch enables enhanced optimizations.
 673     FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true);
 674   }
 675 
 676   setup_compiler_args();
 677 
 678   if (AOTClassLinking) {
 679     // If AOTClassLinking is specified, enable all these optimizations by default.
 680     FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true);
 681     FLAG_SET_ERGO_IF_DEFAULT(ArchiveDynamicProxies, false); // FIXME-merge
 682     FLAG_SET_ERGO_IF_DEFAULT(ArchiveLoaderLookupCache, true);
 683     FLAG_SET_ERGO_IF_DEFAULT(ArchiveReflectionData, true);
 684 
 685     // For simplicity, enable these by default only for new workflow
 686     if (new_aot_flags_used()) {
 687       // These flags will be removed when JDK-8350550 is merged from mainline
 688       FLAG_SET_ERGO_IF_DEFAULT(ArchivePackages, true);
 689       FLAG_SET_ERGO_IF_DEFAULT(ArchiveProtectionDomains, true);
 690     }
 691   } else {
 692     // All of these *might* depend on AOTClassLinking. Better be safe than sorry.
 693     FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);
 694     FLAG_SET_ERGO(ArchiveDynamicProxies, false);
 695     FLAG_SET_ERGO(ArchiveLoaderLookupCache, false);
 696     FLAG_SET_ERGO(ArchivePackages, false);
 697     FLAG_SET_ERGO(ArchiveProtectionDomains, false);
 698     FLAG_SET_ERGO(ArchiveReflectionData, false);
 699 
 700     if (CDSConfig::is_dumping_archive()) {
 701       FLAG_SET_ERGO(AOTRecordTraining, false);
 702       FLAG_SET_ERGO(AOTReplayTraining, false);
 703       AOTCodeCache::disable_caching();
 704     }
 705   }
 706   if (is_dumping_static_archive()) {
 707     if (is_dumping_preimage_static_archive()) {
 708       // Don't tweak execution mode during AOT training run
 709     } else if (is_dumping_final_static_archive()) {
 710       if (Arguments::mode() == Arguments::_comp) {
 711         // AOT assembly phase submits the non-blocking compilation requests
 712         // for methods collected during training run, then waits for all compilations
 713         // to complete. With -Xcomp, we block for each compilation request, which is
 714         // counter-productive. Switching back to mixed mode improves testing time
 715         // with AOT and -Xcomp.
 716         Arguments::set_mode_flags(Arguments::_mixed);
 717       }
 718     } else if (!mode_flag_cmd_line) {
 719       // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
 720       //
 721       // If your classlist is large and you don't care about deterministic dumping, you can use
 722       // -Xshare:dump -Xmixed to improve dumping speed.
 723       Arguments::set_mode_flags(Arguments::_int);
 724     } else if (Arguments::mode() == Arguments::_comp) {
 725       // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
 726       // Java code, so there's not much benefit in running -Xcomp.
 727       aot_log_info(aot)("reduced -Xcomp to -Xmixed for static dumping");
 728       Arguments::set_mode_flags(Arguments::_mixed);
 729     }
 730 
 731     // String deduplication may cause CDS to iterate the strings in different order from one
 732     // run to another which resulting in non-determinstic CDS archives.
 733     // Disable UseStringDeduplication while dumping CDS archive.
 734     UseStringDeduplication = false;
 735   }
 736 
 737   // RecordDynamicDumpInfo is not compatible with ArchiveClassesAtExit
 738   if (ArchiveClassesAtExit != nullptr && RecordDynamicDumpInfo) {
 739     jio_fprintf(defaultStream::output_stream(),
 740                 "-XX:+RecordDynamicDumpInfo cannot be used with -XX:ArchiveClassesAtExit.\n");
 741     return false;
 742   }
 743 
 744   if (ArchiveClassesAtExit == nullptr && !RecordDynamicDumpInfo) {
 745     disable_dumping_dynamic_archive();
 746   } else {
 747     enable_dumping_dynamic_archive(ArchiveClassesAtExit);
 748   }
 749 
 750   if (AutoCreateSharedArchive) {
 751     if (SharedArchiveFile == nullptr) {
 752       aot_log_warning(aot)("-XX:+AutoCreateSharedArchive requires -XX:SharedArchiveFile");
 753       return false;
 754     }
 755     if (ArchiveClassesAtExit != nullptr) {
 756       aot_log_warning(aot)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit");
 757       return false;
 758     }
 759   }
 760 
 761   if (is_using_archive() && patch_mod_javabase) {
 762     Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
 763   }
 764   if (is_using_archive() && has_unsupported_runtime_module_options()) {
 765     UseSharedSpaces = false;
 766   }
 767 
 768   if (is_dumping_archive()) {
 769     // Always verify non-system classes during CDS dump
 770     if (!BytecodeVerificationRemote) {
 771       BytecodeVerificationRemote = true;
 772       aot_log_info(aot)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
 773     }
 774   }
 775 
 776   return true;
 777 }
 778 
 779 void CDSConfig::setup_compiler_args() {
 780   // AOT profiles and AOT-compiled methods are supported only in the JEP 483 workflow.
 781   bool can_dump_profile_and_compiled_code = !CompilerConfig::is_interpreter_only() && AOTClassLinking && new_aot_flags_used();
 782   bool can_use_profile_and_compiled_code = !CompilerConfig::is_interpreter_only() && new_aot_flags_used();
 783 
 784   if (is_dumping_preimage_static_archive() && can_dump_profile_and_compiled_code) {
 785     // JEP 483 workflow -- training
 786     FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
 787     FLAG_SET_ERGO(AOTReplayTraining, false);
 788     AOTCodeCache::disable_caching(); // No AOT code generation during training run
 789     FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
 790   } else if (is_dumping_final_static_archive() && can_dump_profile_and_compiled_code) {
 791     // JEP 483 workflow -- assembly
 792     FLAG_SET_ERGO(AOTRecordTraining, false);
 793     FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
 794     // Generate AOT code only when training data enabled
 795     if (AOTReplayTraining) {
 796       AOTCodeCache::enable_caching();
 797     } else {
 798       AOTCodeCache::disable_caching();
 799     }
 800     FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
 801     disable_dumping_aot_code();     // Don't dump AOT code until metadata and heap are dumped.
 802   } else if (is_using_archive() && can_use_profile_and_compiled_code) {
 803     // JEP 483 workflow -- production
 804     FLAG_SET_ERGO(AOTRecordTraining, false);
 805     FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
 806     // Use AOT code only when training data enabled
 807     if (AOTReplayTraining) {
 808       AOTCodeCache::enable_caching();
 809       FLAG_SET_ERGO_IF_DEFAULT(UseAOTCodeLoadThread, true);
 810     } else {
 811       AOTCodeCache::disable_caching();
 812       FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
 813     }
 814   } else {
 815     FLAG_SET_ERGO(AOTReplayTraining, false);
 816     FLAG_SET_ERGO(AOTRecordTraining, false);
 817     AOTCodeCache::disable_caching();
 818     FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
 819   }
 820 }
 821 
 822 void CDSConfig::prepare_for_dumping() {
 823   assert(CDSConfig::is_dumping_archive(), "sanity");
 824 
 825   if (is_dumping_dynamic_archive() && AOTClassLinking) {
 826     if (FLAG_IS_CMDLINE(AOTClassLinking)) {
 827       log_warning(cds)("AOTClassLinking is not supported for dynamic CDS archive");
 828     }
 829     FLAG_SET_ERGO(AOTClassLinking, false);
 830   }
 831 
 832   if (is_dumping_dynamic_archive() && !is_using_archive()) {
 833     assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives");
 834 
 835     // This could happen if SharedArchiveFile has failed to load:
 836     // - -Xshare:off was specified
 837     // - SharedArchiveFile points to an non-existent file.
 838     // - SharedArchiveFile points to an archive that has failed CRC check
 839     // - SharedArchiveFile is not specified and the VM doesn't have a compatible default archive
 840 
 841 #define __THEMSG " is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."
 842     if (RecordDynamicDumpInfo) {
 843       aot_log_error(aot)("-XX:+RecordDynamicDumpInfo%s", __THEMSG);
 844       AOTMetaspace::unrecoverable_loading_error();
 845     } else {
 846       assert(ArchiveClassesAtExit != nullptr, "sanity");
 847       aot_log_warning(aot)("-XX:ArchiveClassesAtExit" __THEMSG);
 848     }
 849 #undef __THEMSG
 850     disable_dumping_dynamic_archive();
 851     return;
 852   }
 853 
 854   check_unsupported_dumping_module_options();
 855 }
 856 
 857 bool CDSConfig::is_dumping_classic_static_archive() {
 858   return _is_dumping_static_archive &&
 859     !is_dumping_preimage_static_archive() &&
 860     !is_dumping_final_static_archive();
 861 }
 862 
 863 bool CDSConfig::is_dumping_preimage_static_archive() {
 864   return _is_dumping_preimage_static_archive;
 865 }
 866 
 867 bool CDSConfig::is_dumping_preimage_static_archive_with_triggers() {
 868   return (!FLAG_IS_DEFAULT(AOTEndTrainingOnMethodEntry)) && is_dumping_preimage_static_archive();
 869 }
 870 
 871 bool CDSConfig::is_dumping_final_static_archive() {
 872   return _is_dumping_final_static_archive;
 873 }
 874 
 875 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) {
 876   _is_dumping_dynamic_archive = true;
 877   if (output_path == nullptr) {
 878     // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo
 879     // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual
 880     // output path.
 881     _output_archive_path = nullptr;
 882   } else {
 883     _output_archive_path = os::strdup_check_oom(output_path, mtArguments);
 884   }
 885 }
 886 
 887 bool CDSConfig::allow_only_single_java_thread() {
 888   // See comments in JVM_StartThread()
 889   return is_dumping_classic_static_archive() || is_dumping_final_static_archive();
 890 }
 891 
 892 bool CDSConfig::is_logging_dynamic_proxies() {
 893   return ClassListWriter::is_enabled() || is_dumping_preimage_static_archive();
 894 }
 895 
 896 bool CDSConfig::is_using_archive() {
 897   return UseSharedSpaces;
 898 }
 899 
 900 bool CDSConfig::is_using_only_default_archive() {
 901   return is_using_archive() &&
 902          input_static_archive_path() != nullptr &&
 903          default_archive_path() != nullptr &&
 904          strcmp(input_static_archive_path(), default_archive_path()) == 0 &&
 905          input_dynamic_archive_path() == nullptr;
 906 }
 907 
 908 bool CDSConfig::is_logging_lambda_form_invokers() {
 909   return ClassListWriter::is_enabled() || is_dumping_dynamic_archive() || is_dumping_preimage_static_archive();
 910 }
 911 
 912 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
 913   if (is_dumping_final_static_archive()) {
 914     // No need to regenerate -- the lambda form invokers should have been regenerated
 915     // in the preimage archive (if allowed)
 916     return false;
 917   } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) {
 918     // The base archive has aot-linked classes that may have AOT-resolved CP references
 919     // that point to the lambda form invokers in the base archive. Such pointers will
 920     // be invalid if lambda form invokers are regenerated in the dynamic archive.
 921     return false;
 922   } else {
 923     return is_dumping_archive();
 924   }
 925 }
 926 
 927 void CDSConfig::stop_using_optimized_module_handling() {
 928   _is_using_optimized_module_handling = false;
 929   _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling()
 930   _is_using_full_module_graph = false; // This requires is_using_optimized_module_handling()
 931 }
 932 
 933 
 934 CDSConfig::DumperThreadMark::DumperThreadMark(JavaThread* current) {
 935   assert(_dumper_thread == nullptr, "sanity");
 936   _dumper_thread = current;
 937 }
 938 
 939 CDSConfig::DumperThreadMark::~DumperThreadMark() {
 940   assert(_dumper_thread != nullptr, "sanity");
 941   _dumper_thread = nullptr;
 942 }
 943 
 944 bool CDSConfig::current_thread_is_vm_or_dumper() {
 945   Thread* t = Thread::current();
 946   return t->is_VM_thread() || t == _dumper_thread;
 947 }
 948 
 949 bool CDSConfig::current_thread_is_dumper() {
 950   return Thread::current() == _dumper_thread;
 951 }
 952 
 953 const char* CDSConfig::type_of_archive_being_loaded() {
 954   if (is_dumping_final_static_archive()) {
 955     return "AOT configuration file";
 956   } else if (new_aot_flags_used()) {
 957     return "AOT cache";
 958   } else {
 959     return "shared archive file";
 960   }
 961 }
 962 
 963 const char* CDSConfig::type_of_archive_being_written() {
 964   if (is_dumping_preimage_static_archive()) {
 965     return "AOT configuration file";
 966   } else if (new_aot_flags_used()) {
 967     return "AOT cache";
 968   } else {
 969     return "shared archive file";
 970   }
 971 }
 972 
 973 // If an incompatible VM options is found, return a text message that explains why
 974 static const char* check_options_incompatible_with_dumping_heap() {
 975 #if INCLUDE_CDS_JAVA_HEAP
 976   if (!UseCompressedClassPointers) {
 977     return "UseCompressedClassPointers must be true";
 978   }
 979 
 980   return nullptr;
 981 #else
 982   return "JVM not configured for writing Java heap objects";
 983 #endif
 984 }
 985 
 986 void CDSConfig::log_reasons_for_not_dumping_heap() {
 987   const char* reason;
 988 
 989   assert(!is_dumping_heap(), "sanity");
 990 
 991   if (_disable_heap_dumping) {
 992     reason = "Programmatically disabled";
 993   } else {
 994     reason = check_options_incompatible_with_dumping_heap();
 995   }
 996 
 997   assert(reason != nullptr, "sanity");
 998   aot_log_info(aot)("Archived java heap is not supported: %s", reason);
 999 }
1000 
1001 // This is *Legacy* optimization for lambdas before JEP 483. May be removed in the future.
1002 bool CDSConfig::is_dumping_lambdas_in_legacy_mode() {
1003   return !is_dumping_method_handles();
1004 }
1005 
1006 bool CDSConfig::is_preserving_verification_constraints() {
1007   // Verification dependencies are classes used in assignability checks by the
1008   // bytecode verifier. In the following example, the verification dependencies
1009   // for X are A and B.
1010   //
1011   //     class X {
1012   //        A getA() { return new B(); }
1013   //     }
1014   //
1015   // With the AOT cache, we can ensure that all the verification dependencies
1016   // (A and B in the above example) are unconditionally loaded during the bootstrap
1017   // of the production run. This means that if a class was successfully verified
1018   // in the assembly phase, all of the verifier's assignability checks will remain
1019   // valid in the production run, so we don't need to verify aot-linked classes again.
1020 
1021   if (is_dumping_preimage_static_archive()) { // writing AOT config
1022     return AOTClassLinking;
1023   } else if (is_dumping_final_static_archive()) { // writing AOT cache
1024     return is_dumping_aot_linked_classes();
1025   } else if (is_dumping_classic_static_archive()) {
1026     return is_dumping_aot_linked_classes();
1027   } else {
1028     return false;
1029   }
1030 }
1031 
1032 bool CDSConfig::is_old_class_for_verifier(const InstanceKlass* ik) {
1033   return ik->major_version() < 50 /*JAVA_6_VERSION*/;
1034 }
1035 
1036 #if INCLUDE_CDS_JAVA_HEAP
1037 bool CDSConfig::are_vm_options_incompatible_with_dumping_heap() {
1038   return check_options_incompatible_with_dumping_heap() != nullptr;
1039 }
1040 
1041 bool CDSConfig::is_dumping_heap() {
1042   // Note: when dumping preimage static archive, only a very limited set of oops
1043   // are dumped.
1044   if (!is_dumping_static_archive()
1045       || are_vm_options_incompatible_with_dumping_heap()
1046       || _disable_heap_dumping) {
1047     return false;
1048   }
1049   return true;
1050 }
1051 
1052 bool CDSConfig::is_loading_heap() {
1053   return HeapShared::is_archived_heap_in_use();
1054 }
1055 
1056 bool CDSConfig::is_dumping_klass_subgraphs() {
1057   if (is_dumping_classic_static_archive() || is_dumping_final_static_archive()) {
1058     // KlassSubGraphs (see heapShared.cpp) is a legacy mechanism for archiving oops. It
1059     // has been superceded by AOT class linking. This feature is used only when
1060     // AOT class linking is disabled.
1061     //
1062     // KlassSubGraphs are disabled in the preimage static archive, which contains a very
1063     // limited set of oops.
1064     return is_dumping_heap() && !is_dumping_aot_linked_classes();
1065   } else {
1066     return false;
1067   }
1068 }
1069 
1070 bool CDSConfig::is_using_klass_subgraphs() {
1071   return (is_loading_heap() &&
1072           !CDSConfig::is_using_aot_linked_classes() &&
1073           !CDSConfig::is_dumping_final_static_archive());
1074 }
1075 
1076 bool CDSConfig::is_using_full_module_graph() {
1077   if (ClassLoaderDataShared::is_full_module_graph_loaded()) {
1078     return true;
1079   }
1080 
1081   if (!_is_using_full_module_graph) {
1082     return false;
1083   }
1084 
1085   if (is_using_archive() && HeapShared::can_use_archived_heap()) {
1086     // Classes used by the archived full module graph are loaded in JVMTI early phase.
1087     assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()),
1088            "CDS should be disabled if early class hooks are enabled");
1089     return true;
1090   } else {
1091     _is_using_full_module_graph = false;
1092     return false;
1093   }
1094 }
1095 
1096 void CDSConfig::stop_dumping_full_module_graph(const char* reason) {
1097   if (_is_dumping_full_module_graph) {
1098     _is_dumping_full_module_graph = false;
1099     if (reason != nullptr) {
1100       aot_log_info(aot)("full module graph cannot be dumped: %s", reason);
1101     }
1102   }
1103 }
1104 
1105 void CDSConfig::stop_using_full_module_graph(const char* reason) {
1106   assert(!ClassLoaderDataShared::is_full_module_graph_loaded(), "you call this function too late!");
1107   if (_is_using_full_module_graph) {
1108     _is_using_full_module_graph = false;
1109     if (reason != nullptr) {
1110       aot_log_info(aot)("full module graph cannot be loaded: %s", reason);
1111     }
1112   }
1113 }
1114 
1115 bool CDSConfig::is_dumping_aot_linked_classes() {
1116   if (is_dumping_classic_static_archive() || is_dumping_final_static_archive()) {
1117     // FMG is required to guarantee that all cached boot/platform/app classes
1118     // are visible in the production run, so they can be unconditionally
1119     // loaded during VM bootstrap.
1120     return is_dumping_full_module_graph() && AOTClassLinking;
1121   } else {
1122     return false;
1123   }
1124 }
1125 
1126 bool CDSConfig::is_using_aot_linked_classes() {
1127   // Make sure we have the exact same module graph as in the assembly phase, or else
1128   // some aot-linked classes may not be visible so cannot be loaded.
1129   return is_using_full_module_graph() && _has_aot_linked_classes;
1130 }
1131 
1132 bool CDSConfig::is_dumping_dynamic_proxies() {
1133   return is_dumping_full_module_graph() && is_dumping_invokedynamic() && ArchiveDynamicProxies;
1134 }
1135 
1136 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) {
1137   _has_aot_linked_classes |= has_aot_linked_classes;
1138 }
1139 
1140 bool CDSConfig::is_dumping_invokedynamic() {
1141   // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap
1142   // objects used by the archive indy callsites may be replaced at runtime.
1143   return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap();
1144 }
1145 
1146 bool CDSConfig::is_dumping_reflection_data() {
1147   // reflection data use LambdaForm classes
1148   return ArchiveReflectionData && is_dumping_invokedynamic();
1149 }
1150 
1151 // When we are dumping aot-linked classes, we automatically enable the archiving of MethodHandles.
1152 // This will in turn enable the archiving of MethodTypes and hidden
1153 // classes that are used in the implementation of MethodHandles.
1154 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
1155 // and dynamic proxies.
1156 bool CDSConfig::is_dumping_method_handles() {
1157   return is_dumping_aot_linked_classes();
1158 }
1159 
1160 #endif // INCLUDE_CDS_JAVA_HEAP
1161 
1162 // AOT code generation and its archiving is disabled by default.
1163 // We enable it only in the final image dump after the metadata and heap are dumped.
1164 // This affects only JITed code because it may have embedded oops and metadata pointers
1165 // which AOT code encodes as offsets in final CDS archive regions.
1166 
1167 static bool _is_dumping_aot_code = false;
1168 
1169 bool CDSConfig::is_dumping_aot_code() {
1170   return _is_dumping_aot_code;
1171 }
1172 
1173 void CDSConfig::disable_dumping_aot_code() {
1174   _is_dumping_aot_code = false;
1175 }
1176 
1177 void CDSConfig::enable_dumping_aot_code() {
1178   _is_dumping_aot_code = true;
1179 }
1180 
1181 bool CDSConfig::is_dumping_adapters() {
1182   return (AOTAdapterCaching && is_dumping_final_static_archive());
1183 }