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