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