< prev index next >

src/hotspot/share/cds/cdsConfig.cpp

Print this page

   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/archiveHeapLoader.hpp"
  26 #include "cds/cdsConfig.hpp"

  27 #include "cds/classListWriter.hpp"
  28 #include "cds/filemap.hpp"
  29 #include "cds/heapShared.hpp"

  30 #include "classfile/classLoaderDataShared.hpp"
  31 #include "classfile/moduleEntry.hpp"


  32 #include "include/jvm_io.h"
  33 #include "logging/log.hpp"

  34 #include "memory/universe.hpp"
  35 #include "runtime/arguments.hpp"
  36 #include "runtime/globals_extension.hpp"
  37 #include "runtime/java.hpp"
  38 #include "runtime/vmThread.hpp"
  39 #include "utilities/defaultStream.hpp"
  40 #include "utilities/formatBuffer.hpp"
  41 
  42 bool CDSConfig::_is_dumping_static_archive = false;
  43 bool CDSConfig::_is_dumping_preimage_static_archive = false;
  44 bool CDSConfig::_is_dumping_final_static_archive = false;
  45 bool CDSConfig::_is_dumping_dynamic_archive = false;
  46 bool CDSConfig::_is_using_optimized_module_handling = true;
  47 bool CDSConfig::_is_dumping_full_module_graph = true;
  48 bool CDSConfig::_is_using_full_module_graph = true;
  49 bool CDSConfig::_has_aot_linked_classes = false;





  50 bool CDSConfig::_old_cds_flags_used = false;
  51 bool CDSConfig::_new_aot_flags_used = false;

  52 bool CDSConfig::_disable_heap_dumping = false;
  53 
  54 const char* CDSConfig::_default_archive_path = nullptr;
  55 const char* CDSConfig::_input_static_archive_path = nullptr;
  56 const char* CDSConfig::_input_dynamic_archive_path = nullptr;
  57 const char* CDSConfig::_output_archive_path = nullptr;
  58 
  59 JavaThread* CDSConfig::_dumper_thread = nullptr;
  60 
  61 int CDSConfig::get_status() {
  62   assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized");
  63   return (is_dumping_archive()              ? IS_DUMPING_ARCHIVE : 0) |
  64          (is_dumping_method_handles()       ? IS_DUMPING_METHOD_HANDLES : 0) |
  65          (is_dumping_static_archive()       ? IS_DUMPING_STATIC_ARCHIVE : 0) |
  66          (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) |
  67          (is_using_archive()                ? IS_USING_ARCHIVE : 0);




  68 }
  69 
  70 DEBUG_ONLY(static bool _cds_ergo_initialize_started = false);
  71 
  72 void CDSConfig::ergo_initialize() {
  73   DEBUG_ONLY(_cds_ergo_initialize_started = true);
  74 
  75   if (is_dumping_static_archive() && !is_dumping_final_static_archive()) {
  76     // Note: -Xshare and -XX:AOTMode flags are mutually exclusive.
  77     // - Classic workflow: -Xshare:on and -Xshare:dump cannot take effect at the same time.
  78     // - JEP 483 workflow: -XX:AOTMode:record and -XX:AOTMode=on cannot take effect at the same time.
  79     // So we can never come to here with RequireSharedSpaces==true.
  80     assert(!RequireSharedSpaces, "sanity");
  81 
  82     // If dumping the classic archive, or making an AOT training run (dumping a preimage archive),
  83     // for sanity, parse all classes from classfiles.
  84     // TODO: in the future, if we want to support re-training on top of an existing AOT cache, this
  85     // needs to be changed.










  86     UseSharedSpaces = false;
  87   }
  88 
  89   // Initialize shared archive paths which could include both base and dynamic archive paths
  90   // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly.
  91   if (is_dumping_static_archive() || is_using_archive()) {
  92     if (new_aot_flags_used()) {
  93       ergo_init_aot_paths();


  94     } else {
  95       ergo_init_classic_archive_paths();
  96     }
  97   }
  98 
  99   if (!is_dumping_heap()) {
 100     _is_dumping_full_module_graph = false;
 101   }
 102 }
 103 
 104 const char* CDSConfig::default_archive_path() {
 105   // The path depends on UseCompressedOops, etc, which are set by GC ergonomics just
 106   // before CDSConfig::ergo_initialize() is called.
 107   assert(_cds_ergo_initialize_started, "sanity");
 108   if (_default_archive_path == nullptr) {
 109     stringStream tmp;
 110     const char* subdir = WINDOWS_ONLY("bin") NOT_WINDOWS("lib");
 111     tmp.print("%s%s%s%s%s%sclasses", Arguments::get_java_home(), os::file_separator(), subdir,
 112               os::file_separator(), Abstract_VM_Version::vm_variant(), os::file_separator());
 113 #ifdef _LP64

 257         // Check for case (c)
 258         if (RecordDynamicDumpInfo) {
 259           vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
 260                                         SharedArchiveFile);
 261         }
 262         if (ArchiveClassesAtExit != nullptr) {
 263           vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
 264                                         SharedArchiveFile);
 265         }
 266       }
 267 
 268       if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) {
 269           vm_exit_during_initialization(
 270             "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit",
 271             SharedArchiveFile);
 272       }
 273     }
 274   }
 275 }
 276 



 277 void CDSConfig::check_internal_module_property(const char* key, const char* value) {
 278   if (Arguments::is_incompatible_cds_internal_module_property(key)) {
 279     stop_using_optimized_module_handling();
 280     log_info(cds)("optimized module handling: disabled due to incompatible property: %s=%s", key, value);









 281   }
 282 }
 283 
 284 void CDSConfig::check_incompatible_property(const char* key, const char* value) {
 285   static const char* incompatible_properties[] = {
 286     "java.system.class.loader",
 287     "jdk.module.showModuleResolution",
 288     "jdk.module.validation"
 289   };
 290 
 291   for (const char* property : incompatible_properties) {
 292     if (strcmp(key, property) == 0) {
 293       stop_dumping_full_module_graph();
 294       stop_using_full_module_graph();
 295       log_info(cds)("full module graph: disabled due to incompatible property: %s=%s", key, value);
 296       break;
 297     }
 298   }
 299 






 300 }
 301 
 302 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS.
 303 static const char* find_any_unsupported_module_option() {
 304   // Note that arguments.cpp has translated the command-line options into properties. If we find an
 305   // unsupported property, translate it back to its command-line option for better error reporting.
 306 
 307   // The following properties are checked by Arguments::is_internal_module_property() and cannot be
 308   // directly specified in the command-line.
 309   static const char* unsupported_module_properties[] = {
 310     "jdk.module.limitmods",
 311     "jdk.module.upgrade.path",
 312     "jdk.module.patch.0"
 313   };
 314   static const char* unsupported_module_options[] = {
 315     "--limit-modules",
 316     "--upgrade-module-path",
 317     "--patch-module"
 318   };
 319 

 360       if (new_aot_flags_used()) {
 361         log_warning(cds)("AOT cache is disabled when the %s option is specified.", option);
 362       } else {
 363         log_info(cds)("CDS is disabled when the %s option is specified.", option);
 364       }
 365     }
 366     return true;
 367   }
 368   return false;
 369 }
 370 
 371 #define CHECK_NEW_FLAG(f) check_new_flag(FLAG_IS_DEFAULT(f), #f)
 372 
 373 void CDSConfig::check_new_flag(bool new_flag_is_default, const char* new_flag_name) {
 374   if (old_cds_flags_used() && !new_flag_is_default) {
 375     vm_exit_during_initialization(err_msg("Option %s cannot be used at the same time with "
 376                                           "-Xshare:on, -Xshare:auto, -Xshare:off, -Xshare:dump, "
 377                                           "DumpLoadedClassList, SharedClassListFile, or SharedArchiveFile",
 378                                           new_flag_name));
 379   }





 380 }
 381 
 382 #define CHECK_SINGLE_PATH(f) check_flag_single_path(#f, f)
 383 
 384 void CDSConfig::check_flag_single_path(const char* flag_name, const char* value) {
 385   if (value != nullptr && num_archive_paths(value) != 1) {
 386     vm_exit_during_initialization(err_msg("Option %s must specify a single file name", flag_name));
 387   }
 388 }
 389 
 390 void CDSConfig::check_aot_flags() {
 391   if (!FLAG_IS_DEFAULT(DumpLoadedClassList) ||
 392       !FLAG_IS_DEFAULT(SharedClassListFile) ||
 393       !FLAG_IS_DEFAULT(SharedArchiveFile)) {
 394     _old_cds_flags_used = true;
 395   }





 396 
 397   // "New" AOT flags must not be mixed with "classic" flags such as -Xshare:dump
 398   CHECK_NEW_FLAG(AOTCache);

 399   CHECK_NEW_FLAG(AOTConfiguration);
 400   CHECK_NEW_FLAG(AOTMode);
 401 
 402   CHECK_SINGLE_PATH(AOTCache);

 403   CHECK_SINGLE_PATH(AOTConfiguration);
 404 
 405   if (FLAG_IS_DEFAULT(AOTCache) && FLAG_IS_DEFAULT(AOTConfiguration) && FLAG_IS_DEFAULT(AOTMode)) {
 406     // AOTCache/AOTConfiguration/AOTMode not used.
 407     return;
 408   } else {
 409     _new_aot_flags_used = true;







 410   }
 411 



 412   if (FLAG_IS_DEFAULT(AOTMode) || strcmp(AOTMode, "auto") == 0 || strcmp(AOTMode, "on") == 0) {
 413     check_aotmode_auto_or_on();
 414   } else if (strcmp(AOTMode, "off") == 0) {
 415     check_aotmode_off();


 416   } else {
 417     // AOTMode is record or create
 418     if (FLAG_IS_DEFAULT(AOTConfiguration)) {
 419       vm_exit_during_initialization(err_msg("-XX:AOTMode=%s cannot be used without setting AOTConfiguration", AOTMode));
 420     }
 421 
 422     if (strcmp(AOTMode, "record") == 0) {
 423       check_aotmode_record();
 424     } else {
 425       assert(strcmp(AOTMode, "create") == 0, "checked by AOTModeConstraintFunc");
 426       check_aotmode_create();
 427     }
 428   }
 429 }
 430 
 431 void CDSConfig::check_aotmode_off() {
 432   UseSharedSpaces = false;
 433   RequireSharedSpaces = false;
 434 }
 435 
 436 void CDSConfig::check_aotmode_auto_or_on() {
 437   if (!FLAG_IS_DEFAULT(AOTConfiguration)) {
 438     vm_exit_during_initialization("AOTConfiguration can only be used with -XX:AOTMode=record or -XX:AOTMode=create");
 439   }
 440 
 441   UseSharedSpaces = true;
 442   if (FLAG_IS_DEFAULT(AOTMode) || (strcmp(AOTMode, "auto") == 0)) {
 443     RequireSharedSpaces = false;
 444   } else {
 445     assert(strcmp(AOTMode, "on") == 0, "already checked");
 446     RequireSharedSpaces = true;
 447   }
 448 }
 449 
 450 void CDSConfig::check_aotmode_record() {




















 451   if (!FLAG_IS_DEFAULT(AOTCache)) {
 452     vm_exit_during_initialization("AOTCache must not be specified when using -XX:AOTMode=record");
 453   }
 454 
 455   UseSharedSpaces = false;
 456   RequireSharedSpaces = false;
 457   _is_dumping_static_archive = true;
 458   _is_dumping_preimage_static_archive = true;
 459 
 460   // At VM exit, the module graph may be contaminated with program states.
 461   // We will rebuild the module graph when dumping the CDS final image.
 462   disable_heap_dumping();
 463 }
 464 
 465 void CDSConfig::check_aotmode_create() {
 466   if (FLAG_IS_DEFAULT(AOTCache)) {
 467     vm_exit_during_initialization("AOTCache must be specified when using -XX:AOTMode=create");














 468   }
 469 
 470   _is_dumping_final_static_archive = true;
 471   UseSharedSpaces = true;
 472   RequireSharedSpaces = true;
 473 
 474   if (!FileMapInfo::is_preimage_static_archive(AOTConfiguration)) {
 475     vm_exit_during_initialization("Must be a valid AOT configuration generated by the current JVM", AOTConfiguration);
 476   }
 477 
 478   CDSConfig::enable_dumping_static_archive();
 479 }
 480 
 481 void CDSConfig::ergo_init_aot_paths() {
 482   assert(_cds_ergo_initialize_started, "sanity");
 483   if (is_dumping_static_archive()) {
 484     if (is_dumping_preimage_static_archive()) {
 485       _output_archive_path = AOTConfiguration;
 486     } else {
 487       assert(is_dumping_final_static_archive(), "must be");
 488       _input_static_archive_path = AOTConfiguration;
 489       _output_archive_path = AOTCache;
 490     }
 491   } else if (is_using_archive()) {
 492     if (FLAG_IS_DEFAULT(AOTCache)) {
 493       // Only -XX:AOTMode={auto,on} is specified
 494       _input_static_archive_path = default_archive_path();
 495     } else {
 496       _input_static_archive_path = AOTCache;
 497     }
 498   }
 499 }
 500 
 501 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line) {















 502   assert(!_cds_ergo_initialize_started, "This is called earlier than CDSConfig::ergo_initialize()");
 503 
 504   check_aot_flags();
 505 
 506   if (!FLAG_IS_DEFAULT(AOTMode)) {
 507     // Using any form of the new AOTMode switch enables enhanced optimizations.
 508     FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true);
 509   }
 510 












 511   if (AOTClassLinking) {
 512     // If AOTClassLinking is specified, enable all AOT optimizations by default.
 513     FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true);





 514   } else {
 515     // AOTInvokeDynamicLinking depends on AOTClassLinking.
 516     FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);

















 517   }
 518 





 519   if (is_dumping_static_archive()) {
 520     if (is_dumping_preimage_static_archive()) {
 521       // Don't tweak execution mode
 522     } else if (!mode_flag_cmd_line) {
 523       // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
 524       //
 525       // If your classlist is large and you don't care about deterministic dumping, you can use
 526       // -Xshare:dump -Xmixed to improve dumping speed.
 527       Arguments::set_mode_flags(Arguments::_int);
 528     } else if (Arguments::mode() == Arguments::_comp) {
 529       // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
 530       // Java code, so there's not much benefit in running -Xcomp.
 531       log_info(cds)("reduced -Xcomp to -Xmixed for static dumping");
 532       Arguments::set_mode_flags(Arguments::_mixed);
 533     }
 534 
 535     // String deduplication may cause CDS to iterate the strings in different order from one
 536     // run to another which resulting in non-determinstic CDS archives.
 537     // Disable UseStringDeduplication while dumping CDS archive.
 538     UseStringDeduplication = false;
 539 
 540     // Don't use SoftReferences so that objects used by java.lang.invoke tables can be archived.
 541     Arguments::PropertyList_add(new SystemProperty("java.lang.invoke.MethodHandleNatives.USE_SOFT_CACHE", "false", false));
 542   }
 543 
 544   // RecordDynamicDumpInfo is not compatible with ArchiveClassesAtExit
 545   if (ArchiveClassesAtExit != nullptr && RecordDynamicDumpInfo) {
 546     jio_fprintf(defaultStream::output_stream(),
 547                 "-XX:+RecordDynamicDumpInfo cannot be used with -XX:ArchiveClassesAtExit.\n");
 548     return false;
 549   }
 550 
 551   if (ArchiveClassesAtExit == nullptr && !RecordDynamicDumpInfo) {
 552     disable_dumping_dynamic_archive();
 553   } else {
 554     enable_dumping_dynamic_archive(ArchiveClassesAtExit);
 555   }
 556 
 557   if (AutoCreateSharedArchive) {
 558     if (SharedArchiveFile == nullptr) {
 559       log_warning(cds)("-XX:+AutoCreateSharedArchive requires -XX:SharedArchiveFile");
 560       return false;
 561     }

 563       log_warning(cds)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit");
 564       return false;
 565     }
 566   }
 567 
 568   if (is_using_archive() && patch_mod_javabase) {
 569     Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
 570   }
 571   if (is_using_archive() && has_unsupported_runtime_module_options()) {
 572     UseSharedSpaces = false;
 573   }
 574 
 575   if (is_dumping_archive()) {
 576     // Always verify non-system classes during CDS dump
 577     if (!BytecodeVerificationRemote) {
 578       BytecodeVerificationRemote = true;
 579       log_info(cds)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
 580     }
 581   }
 582 









































































































































 583   return true;
 584 }
 585 
 586 void CDSConfig::prepare_for_dumping() {
 587   assert(CDSConfig::is_dumping_archive(), "sanity");
 588 
 589   if (is_dumping_dynamic_archive() && !is_using_archive()) {
 590     assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives");
 591 
 592     // This could happen if SharedArchiveFile has failed to load:
 593     // - -Xshare:off was specified
 594     // - SharedArchiveFile points to an non-existent file.
 595     // - SharedArchiveFile points to an archive that has failed CRC check
 596     // - SharedArchiveFile is not specified and the VM doesn't have a compatible default archive
 597 
 598 #define __THEMSG " is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."
 599     if (RecordDynamicDumpInfo) {
 600       log_error(cds)("-XX:+RecordDynamicDumpInfo%s", __THEMSG);
 601       MetaspaceShared::unrecoverable_loading_error();
 602     } else {

 604       log_warning(cds)("-XX:ArchiveClassesAtExit" __THEMSG);
 605     }
 606 #undef __THEMSG
 607     disable_dumping_dynamic_archive();
 608     return;
 609   }
 610 
 611   check_unsupported_dumping_module_options();
 612 }
 613 
 614 bool CDSConfig::is_dumping_classic_static_archive() {
 615   return _is_dumping_static_archive &&
 616     !is_dumping_preimage_static_archive() &&
 617     !is_dumping_final_static_archive();
 618 }
 619 
 620 bool CDSConfig::is_dumping_preimage_static_archive() {
 621   return _is_dumping_preimage_static_archive;
 622 }
 623 




 624 bool CDSConfig::is_dumping_final_static_archive() {
 625   return _is_dumping_final_static_archive;
 626 }
 627 
 628 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) {
 629   _is_dumping_dynamic_archive = true;
 630   if (output_path == nullptr) {
 631     // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo
 632     // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual
 633     // output path.
 634     _output_archive_path = nullptr;
 635   } else {
 636     _output_archive_path = os::strdup_check_oom(output_path, mtArguments);
 637   }
 638 }
 639 
 640 bool CDSConfig::allow_only_single_java_thread() {
 641   // See comments in JVM_StartThread()
 642   return is_dumping_classic_static_archive() || is_dumping_final_static_archive();
 643 }
 644 














 645 bool CDSConfig::is_using_archive() {
 646   return UseSharedSpaces;
 647 }
 648 
 649 bool CDSConfig::is_logging_lambda_form_invokers() {
 650   return ClassListWriter::is_enabled() || is_dumping_dynamic_archive();
 651 }
 652 
 653 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
 654   if (is_dumping_final_static_archive()) {
 655     // No need to regenerate -- the lambda form invokers should have been regenerated
 656     // in the preimage archive (if allowed)
 657     return false;
 658   } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) {
 659     // The base archive has aot-linked classes that may have AOT-resolved CP references
 660     // that point to the lambda form invokers in the base archive. Such pointers will
 661     // be invalid if lambda form invokers are regenerated in the dynamic archive.
 662     return false;
 663   } else if (CDSConfig::is_dumping_method_handles()) {
 664     // Work around JDK-8310831, as some methods in lambda form holder classes may not get generated.
 665     return false;
 666   } else {
 667     return is_dumping_archive();
 668   }
 669 }
 670 

 801     _is_using_full_module_graph = false;
 802     if (reason != nullptr) {
 803       log_info(cds)("full module graph cannot be loaded: %s", reason);
 804     }
 805   }
 806 }
 807 
 808 bool CDSConfig::is_dumping_aot_linked_classes() {
 809   if (is_dumping_preimage_static_archive()) {
 810     return false;
 811   } else if (is_dumping_dynamic_archive()) {
 812     return is_using_full_module_graph() && AOTClassLinking;
 813   } else if (is_dumping_static_archive()) {
 814     return is_dumping_full_module_graph() && AOTClassLinking;
 815   } else {
 816     return false;
 817   }
 818 }
 819 
 820 bool CDSConfig::is_using_aot_linked_classes() {





 821   // Make sure we have the exact same module graph as in the assembly phase, or else
 822   // some aot-linked classes may not be visible so cannot be loaded.
 823   return is_using_full_module_graph() && _has_aot_linked_classes;
 824 }
 825 




 826 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) {
 827   _has_aot_linked_classes |= has_aot_linked_classes;
 828 }
 829 
 830 bool CDSConfig::is_initing_classes_at_dump_time() {
 831   return is_dumping_heap() && is_dumping_aot_linked_classes();
 832 }
 833 
 834 bool CDSConfig::is_dumping_invokedynamic() {
 835   // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap
 836   // objects used by the archive indy callsites may be replaced at runtime.
 837   return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap();
 838 }
 839 































 840 // When we are dumping aot-linked classes and we are able to write archived heap objects, we automatically
 841 // enable the archiving of MethodHandles. This will in turn enable the archiving of MethodTypes and hidden
 842 // classes that are used in the implementation of MethodHandles.
 843 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
 844 // and dynamic proxies.
 845 bool CDSConfig::is_dumping_method_handles() {
 846   return is_initing_classes_at_dump_time();
 847 }
 848 
 849 #endif // INCLUDE_CDS_JAVA_HEAP

























   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/archiveHeapLoader.hpp"
  26 #include "cds/cdsConfig.hpp"
  27 #include "cds/cds_globals.hpp"
  28 #include "cds/classListWriter.hpp"
  29 #include "cds/filemap.hpp"
  30 #include "cds/heapShared.hpp"
  31 #include "cds/metaspaceShared.hpp"
  32 #include "classfile/classLoaderDataShared.hpp"
  33 #include "classfile/moduleEntry.hpp"
  34 #include "classfile/systemDictionaryShared.hpp"
  35 #include "code/SCCache.hpp"
  36 #include "include/jvm_io.h"
  37 #include "logging/log.hpp"
  38 #include "prims/jvmtiExport.hpp"
  39 #include "memory/universe.hpp"
  40 #include "runtime/arguments.hpp"
  41 #include "runtime/globals_extension.hpp"
  42 #include "runtime/java.hpp"
  43 #include "runtime/vmThread.hpp"
  44 #include "utilities/defaultStream.hpp"
  45 #include "utilities/formatBuffer.hpp"
  46 
  47 bool CDSConfig::_is_dumping_static_archive = false;
  48 bool CDSConfig::_is_dumping_preimage_static_archive = false;
  49 bool CDSConfig::_is_dumping_final_static_archive = false;
  50 bool CDSConfig::_is_dumping_dynamic_archive = false;
  51 bool CDSConfig::_is_using_optimized_module_handling = true;
  52 bool CDSConfig::_is_dumping_full_module_graph = true;
  53 bool CDSConfig::_is_using_full_module_graph = true;
  54 bool CDSConfig::_has_aot_linked_classes = false;
  55 bool CDSConfig::_is_one_step_training = false;
  56 bool CDSConfig::_has_temp_aot_config_file = false;
  57 bool CDSConfig::_is_loading_packages = false;
  58 bool CDSConfig::_is_loading_protection_domains = false;
  59 bool CDSConfig::_is_security_manager_allowed = false;
  60 bool CDSConfig::_old_cds_flags_used = false;
  61 bool CDSConfig::_new_aot_flags_used = false;
  62 bool CDSConfig::_experimental_leyden_flags_used = false;
  63 bool CDSConfig::_disable_heap_dumping = false;
  64 
  65 const char* CDSConfig::_default_archive_path = nullptr;
  66 const char* CDSConfig::_input_static_archive_path = nullptr;
  67 const char* CDSConfig::_input_dynamic_archive_path = nullptr;
  68 const char* CDSConfig::_output_archive_path = nullptr;
  69 
  70 JavaThread* CDSConfig::_dumper_thread = nullptr;
  71 
  72 int CDSConfig::get_status() {
  73   assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized");
  74   return (is_dumping_archive()              ? IS_DUMPING_ARCHIVE : 0) |
  75          (is_dumping_method_handles()       ? IS_DUMPING_METHOD_HANDLES : 0) |
  76          (is_dumping_static_archive()       ? IS_DUMPING_STATIC_ARCHIVE : 0) |
  77          (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) |
  78          (is_using_archive()                ? IS_USING_ARCHIVE : 0) |
  79          (is_dumping_heap()                 ? IS_DUMPING_HEAP : 0) |
  80          (is_logging_dynamic_proxies()      ? IS_LOGGING_DYNAMIC_PROXIES : 0) |
  81          (is_dumping_packages()             ? IS_DUMPING_PACKAGES : 0) |
  82          (is_dumping_protection_domains()   ? IS_DUMPING_PROTECTION_DOMAINS : 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     // If dumping the classic archive, or making an AOT training run (dumping a preimage archive),
  92     // for sanity, parse all classes from classfiles.
  93     // TODO: in the future, if we want to support re-training on top of an existing AOT cache, this
  94     // needs to be changed.
  95     if (RequireSharedSpaces) {
  96       if (is_experimental_leyden_workflow()) {
  97         log_info(cds)("-Xshare:on flag is ignored when creating a CacheDataStore");
  98       } else {
  99         // -Xshare and -XX:AOTMode flags are mutually exclusive:
 100         //   Class workflow: -Xshare:on and -Xshare:dump cannot take effect at the same time.
 101         //   JEP 483 workflow: -XX:AOTMode:record and -XX:AOTMode=on cannot take effect at the same time.
 102         ShouldNotReachHere();
 103       }
 104     }
 105     UseSharedSpaces = false;
 106   }
 107 
 108   // Initialize shared archive paths which could include both base and dynamic archive paths
 109   // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly.
 110   if (is_dumping_static_archive() || is_using_archive()) {
 111     if (new_aot_flags_used()) {
 112       ergo_init_aot_paths();
 113     } else if (is_experimental_leyden_workflow()) {
 114       ergo_init_experimental_leyden_paths();
 115     } else {
 116       ergo_init_classic_archive_paths();
 117     }
 118   }
 119 
 120   if (!is_dumping_heap()) {
 121     _is_dumping_full_module_graph = false;
 122   }
 123 }
 124 
 125 const char* CDSConfig::default_archive_path() {
 126   // The path depends on UseCompressedOops, etc, which are set by GC ergonomics just
 127   // before CDSConfig::ergo_initialize() is called.
 128   assert(_cds_ergo_initialize_started, "sanity");
 129   if (_default_archive_path == nullptr) {
 130     stringStream tmp;
 131     const char* subdir = WINDOWS_ONLY("bin") NOT_WINDOWS("lib");
 132     tmp.print("%s%s%s%s%s%sclasses", Arguments::get_java_home(), os::file_separator(), subdir,
 133               os::file_separator(), Abstract_VM_Version::vm_variant(), os::file_separator());
 134 #ifdef _LP64

 278         // Check for case (c)
 279         if (RecordDynamicDumpInfo) {
 280           vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
 281                                         SharedArchiveFile);
 282         }
 283         if (ArchiveClassesAtExit != nullptr) {
 284           vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
 285                                         SharedArchiveFile);
 286         }
 287       }
 288 
 289       if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) {
 290           vm_exit_during_initialization(
 291             "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit",
 292             SharedArchiveFile);
 293       }
 294     }
 295   }
 296 }
 297 
 298 static char* bad_module_prop_key   = nullptr;
 299 static char* bad_module_prop_value = nullptr;
 300 
 301 void CDSConfig::check_internal_module_property(const char* key, const char* value) {
 302   if (Arguments::is_incompatible_cds_internal_module_property(key)) {
 303     stop_using_optimized_module_handling();
 304     if (bad_module_prop_key == nullptr) {
 305       // We don't want to print an unconditional warning here, as we are still processing the command line.
 306       // A later argument may specify something like -Xshare:off, which makes such a warning irrelevant.
 307       //
 308       // Instead, we save the info so we can warn when necessary: we are doing it only during CacheDataStore
 309       // creation for now, but could add it to other places.
 310       bad_module_prop_key   = os::strdup(key);
 311       bad_module_prop_value = os::strdup(value);
 312     }
 313     log_info(cds)("optimized module handling/full module graph: disabled due to incompatible property: %s=%s", key, value);
 314   }
 315 }
 316 
 317 void CDSConfig::check_incompatible_property(const char* key, const char* value) {
 318   static const char* incompatible_properties[] = {
 319     "java.system.class.loader",
 320     "jdk.module.showModuleResolution",
 321     "jdk.module.validation"
 322   };
 323 
 324   for (const char* property : incompatible_properties) {
 325     if (strcmp(key, property) == 0) {
 326       stop_dumping_full_module_graph();
 327       stop_using_full_module_graph();
 328       log_info(cds)("full module graph: disabled due to incompatible property: %s=%s", key, value);
 329       break;
 330     }
 331   }
 332 
 333   // Match the logic in java/lang/System.java, but we need to know this before the System class is initialized.
 334   if (strcmp(key, "java.security.manager") == 0) {
 335     if (strcmp(value, "disallowed") != 0) {
 336       _is_security_manager_allowed = true;
 337     }
 338   }
 339 }
 340 
 341 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS.
 342 static const char* find_any_unsupported_module_option() {
 343   // Note that arguments.cpp has translated the command-line options into properties. If we find an
 344   // unsupported property, translate it back to its command-line option for better error reporting.
 345 
 346   // The following properties are checked by Arguments::is_internal_module_property() and cannot be
 347   // directly specified in the command-line.
 348   static const char* unsupported_module_properties[] = {
 349     "jdk.module.limitmods",
 350     "jdk.module.upgrade.path",
 351     "jdk.module.patch.0"
 352   };
 353   static const char* unsupported_module_options[] = {
 354     "--limit-modules",
 355     "--upgrade-module-path",
 356     "--patch-module"
 357   };
 358 

 399       if (new_aot_flags_used()) {
 400         log_warning(cds)("AOT cache is disabled when the %s option is specified.", option);
 401       } else {
 402         log_info(cds)("CDS is disabled when the %s option is specified.", option);
 403       }
 404     }
 405     return true;
 406   }
 407   return false;
 408 }
 409 
 410 #define CHECK_NEW_FLAG(f) check_new_flag(FLAG_IS_DEFAULT(f), #f)
 411 
 412 void CDSConfig::check_new_flag(bool new_flag_is_default, const char* new_flag_name) {
 413   if (old_cds_flags_used() && !new_flag_is_default) {
 414     vm_exit_during_initialization(err_msg("Option %s cannot be used at the same time with "
 415                                           "-Xshare:on, -Xshare:auto, -Xshare:off, -Xshare:dump, "
 416                                           "DumpLoadedClassList, SharedClassListFile, or SharedArchiveFile",
 417                                           new_flag_name));
 418   }
 419   if (experimental_leyden_flags_used() && !new_flag_is_default) {
 420     vm_exit_during_initialization(err_msg("Option %s cannot be used at the same time with "
 421                                           "CacheDataStore, CDSManualFinalImage, or CDSPreimage",
 422                                           new_flag_name));
 423   }
 424 }
 425 
 426 #define CHECK_SINGLE_PATH(f) check_flag_single_path(#f, f)
 427 
 428 void CDSConfig::check_flag_single_path(const char* flag_name, const char* value) {
 429   if (value != nullptr && num_archive_paths(value) != 1) {
 430     vm_exit_during_initialization(err_msg("Option %s must specify a single file name", flag_name));
 431   }
 432 }
 433 
 434 void CDSConfig::check_aot_flags() {
 435   if (!FLAG_IS_DEFAULT(DumpLoadedClassList) ||
 436       !FLAG_IS_DEFAULT(SharedClassListFile) ||
 437       !FLAG_IS_DEFAULT(SharedArchiveFile)) {
 438     _old_cds_flags_used = true;
 439   }
 440   if (!FLAG_IS_DEFAULT(CacheDataStore) ||
 441       !FLAG_IS_DEFAULT(CDSManualFinalImage) ||
 442       !FLAG_IS_DEFAULT(CDSPreimage)) {
 443     _experimental_leyden_flags_used = true;
 444   }
 445 
 446   // "New" AOT flags must not be mixed with "classic" CDS flags such as -Xshare:dump
 447   CHECK_NEW_FLAG(AOTCache);
 448   CHECK_NEW_FLAG(AOTCacheOutput);
 449   CHECK_NEW_FLAG(AOTConfiguration);
 450   CHECK_NEW_FLAG(AOTMode);
 451 
 452   CHECK_SINGLE_PATH(AOTCache);
 453   CHECK_SINGLE_PATH(AOTCacheOutput);
 454   CHECK_SINGLE_PATH(AOTConfiguration);
 455 
 456   if (FLAG_IS_DEFAULT(AOTCache) &&
 457       FLAG_IS_DEFAULT(AOTMode)) {
 458     bool has_cache_output = !FLAG_IS_DEFAULT(AOTCacheOutput);
 459     bool has_config = !FLAG_IS_DEFAULT(AOTConfiguration);
 460     if (!has_cache_output && !has_config) {
 461       // AOT flags are not used. Use classic CDS workflow
 462       return;
 463     } else if (has_cache_output) {
 464       // If AOTCacheOutput has been set, default mode is "record".
 465       // Default value for AOTConfiguration, if necessary, will be assigned in check_aotmode_record().
 466       FLAG_SET_ERGO(AOTMode, "record");
 467     }
 468   }
 469 
 470   // At least one AOT flag has been used
 471  _new_aot_flags_used = true;
 472 
 473   if (FLAG_IS_DEFAULT(AOTMode) || strcmp(AOTMode, "auto") == 0 || strcmp(AOTMode, "on") == 0) {
 474     check_aotmode_auto_or_on();
 475   } else if (strcmp(AOTMode, "off") == 0) {
 476     check_aotmode_off();
 477   } else if (strcmp(AOTMode, "record") == 0) {
 478     check_aotmode_record();
 479   } else {
 480     assert(strcmp(AOTMode, "create") == 0, "checked by AOTModeConstraintFunc");
 481     check_aotmode_create();









 482   }
 483 }
 484 
 485 void CDSConfig::check_aotmode_off() {
 486   UseSharedSpaces = false;
 487   RequireSharedSpaces = false;
 488 }
 489 
 490 void CDSConfig::check_aotmode_auto_or_on() {
 491   if (!FLAG_IS_DEFAULT(AOTConfiguration)) {
 492     vm_exit_during_initialization("AOTConfiguration can only be used with -XX:AOTMode=record or -XX:AOTMode=create");
 493   }
 494 
 495   UseSharedSpaces = true;
 496   if (FLAG_IS_DEFAULT(AOTMode) || (strcmp(AOTMode, "auto") == 0)) {
 497     RequireSharedSpaces = false;
 498   } else {
 499     assert(strcmp(AOTMode, "on") == 0, "already checked");
 500     RequireSharedSpaces = true;
 501   }
 502 }
 503 
 504 void CDSConfig::check_aotmode_record() {
 505   bool has_config = !FLAG_IS_DEFAULT(AOTConfiguration);
 506   bool has_output = !FLAG_IS_DEFAULT(AOTCacheOutput);
 507 
 508   if (has_output) {
 509     _is_one_step_training = true;
 510     if (!has_config) {
 511       // Too early; can't use resource allocation yet.
 512       size_t len = strlen(AOTCacheOutput) + 10;
 513       char* temp = AllocateHeap(len, mtArguments);
 514       jio_snprintf(temp, len, "%s.config", AOTCacheOutput);
 515       FLAG_SET_ERGO(AOTConfiguration, temp);
 516       FreeHeap(temp);
 517       _has_temp_aot_config_file = true;
 518     }
 519   } else {
 520     if (!has_config) {
 521       vm_exit_during_initialization("-XX:AOTMode=record cannot be used without setting AOTCacheOutput or AOTConfiguration");
 522     }
 523   }
 524 
 525   if (!FLAG_IS_DEFAULT(AOTCache)) {
 526     vm_exit_during_initialization("AOTCache must not be specified when using -XX:AOTMode=record");
 527   }
 528 
 529   UseSharedSpaces = false;
 530   RequireSharedSpaces = false;
 531   _is_dumping_static_archive = true;
 532   _is_dumping_preimage_static_archive = true;
 533 
 534   // At VM exit, the module graph may be contaminated with program states.
 535   // We will rebuild the module graph when dumping the CDS final image.
 536   disable_heap_dumping();
 537 }
 538 
 539 void CDSConfig::check_aotmode_create() {
 540   if (FLAG_IS_DEFAULT(AOTConfiguration)) {
 541     vm_exit_during_initialization("-XX:AOTMode=create cannot be used without setting AOTConfiguration");
 542   }
 543 
 544   bool has_cache = !FLAG_IS_DEFAULT(AOTCache);
 545   bool has_cache_output = !FLAG_IS_DEFAULT(AOTCacheOutput);
 546 
 547   if (!has_cache && !has_cache_output) {
 548     vm_exit_during_initialization("AOTCache or AOTCacheOutput must be specified when using -XX:AOTMode=create");
 549   } else if (has_cache && has_cache_output && strcmp(AOTCache, AOTCacheOutput) != 0) {
 550     vm_exit_during_initialization("AOTCache and AOTCacheOutput have different values");
 551   }
 552 
 553   if (!has_cache) {
 554     precond(has_cache_output);
 555     FLAG_SET_ERGO(AOTCache, AOTCacheOutput);
 556   }
 557 
 558   _is_dumping_final_static_archive = true;
 559   UseSharedSpaces = true;
 560   RequireSharedSpaces = true;
 561 
 562   if (!FileMapInfo::is_preimage_static_archive(AOTConfiguration)) {
 563     vm_exit_during_initialization("Must be a valid AOT configuration generated by the current JVM", AOTConfiguration);
 564   }
 565 
 566   CDSConfig::enable_dumping_static_archive();
 567 }
 568 
 569 void CDSConfig::ergo_init_aot_paths() {
 570   assert(_cds_ergo_initialize_started, "sanity");
 571   if (is_dumping_static_archive()) {
 572     if (is_dumping_preimage_static_archive()) {
 573       _output_archive_path = AOTConfiguration;
 574     } else {
 575       assert(is_dumping_final_static_archive(), "must be");
 576       _input_static_archive_path = AOTConfiguration;
 577       _output_archive_path = AOTCache;
 578     }
 579   } else if (is_using_archive()) {
 580     if (FLAG_IS_DEFAULT(AOTCache)) {
 581       // Only -XX:AOTMode={auto,on} is specified
 582       _input_static_archive_path = default_archive_path();
 583     } else {
 584       _input_static_archive_path = AOTCache;
 585     }
 586   }
 587 }
 588 
 589 void CDSConfig::ergo_init_experimental_leyden_paths() {
 590   assert(_cds_ergo_initialize_started, "sanity");
 591   if (is_dumping_static_archive()) {
 592     if (is_dumping_preimage_static_archive()) {
 593       _output_archive_path = CDSPreimage;
 594     } else {
 595       assert(is_dumping_final_static_archive(), "must be");
 596       _input_static_archive_path = CDSPreimage;
 597       _output_archive_path = CacheDataStore;
 598     }
 599   } else if (is_using_archive()) {
 600     _input_static_archive_path = CacheDataStore;
 601   }
 602 }
 603 
 604 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line, bool xshare_auto_cmd_line) {
 605   assert(!_cds_ergo_initialize_started, "This is called earlier than CDSConfig::ergo_initialize()");
 606 
 607   check_aot_flags();
 608 
 609   if (!FLAG_IS_DEFAULT(AOTMode)) {
 610     // Using any form of the new AOTMode switch enables enhanced optimizations.
 611     FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true);
 612   }
 613 
 614   if (CacheDataStore != nullptr) {
 615     if (!setup_experimental_leyden_workflow(xshare_auto_cmd_line)) {
 616       return false;
 617     }
 618   } else {
 619     if (CDSPreimage != nullptr) {
 620       vm_exit_during_initialization("CDSPreimage must be specified only when CacheDataStore is specified");
 621     }
 622 
 623     setup_compiler_args();
 624   }
 625 
 626   if (AOTClassLinking) {
 627     // If AOTClassLinking is specified, enable all these optimizations by default.
 628     FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true);
 629     FLAG_SET_ERGO_IF_DEFAULT(ArchiveDynamicProxies, true);
 630     FLAG_SET_ERGO_IF_DEFAULT(ArchiveLoaderLookupCache, true);
 631     FLAG_SET_ERGO_IF_DEFAULT(ArchivePackages, true);
 632     FLAG_SET_ERGO_IF_DEFAULT(ArchiveProtectionDomains, true);
 633     FLAG_SET_ERGO_IF_DEFAULT(ArchiveReflectionData, true);
 634   } else {
 635     // All of these *might* depend on AOTClassLinking. Better be safe than sorry.
 636     FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);
 637     FLAG_SET_ERGO(ArchiveDynamicProxies, false);
 638     FLAG_SET_ERGO(ArchiveLoaderLookupCache, false);
 639     FLAG_SET_ERGO(ArchivePackages, false);
 640     FLAG_SET_ERGO(ArchiveProtectionDomains, false);
 641     FLAG_SET_ERGO(ArchiveReflectionData, false);
 642 
 643     if (CDSConfig::is_dumping_archive()) {
 644       FLAG_SET_ERGO(AOTRecordTraining, false);
 645       FLAG_SET_ERGO(AOTReplayTraining, false);
 646       FLAG_SET_ERGO(StoreCachedCode, false);
 647       FLAG_SET_ERGO(LoadCachedCode, false);
 648     }
 649   }
 650 
 651   if (StoreCachedCode) {
 652     log_info(cds)("ArchiveAdapters is enabled");
 653     FLAG_SET_ERGO_IF_DEFAULT(ArchiveAdapters, true);
 654   }
 655 
 656 #ifdef _WINDOWS
 657   // This optimization is not working on Windows for some reason. See JDK-8338604.
 658   FLAG_SET_ERGO(ArchiveReflectionData, false);
 659 #endif
 660 
 661   if (is_dumping_static_archive()) {
 662     if (is_dumping_preimage_static_archive() || is_dumping_final_static_archive()) {
 663       // Don't tweak execution mode
 664     } else if (!mode_flag_cmd_line) {
 665       // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
 666       //
 667       // If your classlist is large and you don't care about deterministic dumping, you can use
 668       // -Xshare:dump -Xmixed to improve dumping speed.
 669       Arguments::set_mode_flags(Arguments::_int);
 670     } else if (Arguments::mode() == Arguments::_comp) {
 671       // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
 672       // Java code, so there's not much benefit in running -Xcomp.
 673       log_info(cds)("reduced -Xcomp to -Xmixed for static dumping");
 674       Arguments::set_mode_flags(Arguments::_mixed);
 675     }
 676 
 677     // String deduplication may cause CDS to iterate the strings in different order from one
 678     // run to another which resulting in non-determinstic CDS archives.
 679     // Disable UseStringDeduplication while dumping CDS archive.
 680     UseStringDeduplication = false;



 681   }
 682 
 683   // RecordDynamicDumpInfo is not compatible with ArchiveClassesAtExit
 684   if (ArchiveClassesAtExit != nullptr && RecordDynamicDumpInfo) {
 685     jio_fprintf(defaultStream::output_stream(),
 686                 "-XX:+RecordDynamicDumpInfo cannot be used with -XX:ArchiveClassesAtExit.\n");
 687     return false;
 688   }
 689 
 690   if (ArchiveClassesAtExit == nullptr && !RecordDynamicDumpInfo) {
 691     disable_dumping_dynamic_archive();
 692   } else {
 693     enable_dumping_dynamic_archive(ArchiveClassesAtExit);
 694   }
 695 
 696   if (AutoCreateSharedArchive) {
 697     if (SharedArchiveFile == nullptr) {
 698       log_warning(cds)("-XX:+AutoCreateSharedArchive requires -XX:SharedArchiveFile");
 699       return false;
 700     }

 702       log_warning(cds)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit");
 703       return false;
 704     }
 705   }
 706 
 707   if (is_using_archive() && patch_mod_javabase) {
 708     Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
 709   }
 710   if (is_using_archive() && has_unsupported_runtime_module_options()) {
 711     UseSharedSpaces = false;
 712   }
 713 
 714   if (is_dumping_archive()) {
 715     // Always verify non-system classes during CDS dump
 716     if (!BytecodeVerificationRemote) {
 717       BytecodeVerificationRemote = true;
 718       log_info(cds)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
 719     }
 720   }
 721 
 722   if (AOTClassLinking) {
 723     if (is_dumping_final_static_archive() && !is_dumping_full_module_graph()) {
 724       if (bad_module_prop_key != nullptr) {
 725         log_warning(cds)("optimized module handling/full module graph: disabled due to incompatible property: %s=%s",
 726                          bad_module_prop_key, bad_module_prop_value);
 727       }
 728       if (is_experimental_leyden_workflow()) {
 729         vm_exit_during_initialization("CacheDataStore cannot be created because AOTClassLinking is enabled but full module graph is disabled");
 730       } else {
 731         vm_exit_during_initialization("AOT cache cannot be created because AOTClassLinking is enabled but full module graph is disabled");
 732       }
 733     }
 734   }
 735 
 736   return true;
 737 }
 738 
 739 void CDSConfig::setup_compiler_args() {
 740   // AOT profiles and AOT-compiled methods are supported only in the JEP 483 workflow.
 741   bool can_dump_profile_and_compiled_code = AOTClassLinking && new_aot_flags_used();
 742 
 743   if (is_dumping_preimage_static_archive() && can_dump_profile_and_compiled_code) {
 744     // JEP 483 workflow -- training
 745     FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
 746     FLAG_SET_ERGO(AOTReplayTraining, false);
 747     FLAG_SET_ERGO(StoreCachedCode, false);
 748     FLAG_SET_ERGO(LoadCachedCode, false);
 749   } else if (is_dumping_final_static_archive() && can_dump_profile_and_compiled_code) {
 750     // JEP 483 workflow -- assembly
 751     FLAG_SET_ERGO(AOTRecordTraining, false); // This will be updated inside MetaspaceShared::preload_and_dump()
 752     FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
 753     FLAG_SET_ERGO_IF_DEFAULT(StoreCachedCode, true);
 754     FLAG_SET_ERGO(LoadCachedCode, false);
 755     disable_dumping_cached_code(); // Cannot dump cached code until metadata and heap are dumped.
 756   } else if (is_using_archive() && new_aot_flags_used()) {
 757     // JEP 483 workflow -- production
 758     FLAG_SET_ERGO(AOTRecordTraining, false);
 759     FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
 760     FLAG_SET_ERGO(StoreCachedCode, false);
 761     FLAG_SET_ERGO_IF_DEFAULT(LoadCachedCode, true);
 762 
 763     if (UseSharedSpaces && FLAG_IS_DEFAULT(AOTMode)) {
 764       log_info(cds)("Enabled -XX:AOTMode=on by default for troubleshooting Leyden prototype");
 765       RequireSharedSpaces = true;
 766     }
 767   } else {
 768     FLAG_SET_ERGO(AOTReplayTraining, false);
 769     FLAG_SET_ERGO(AOTRecordTraining, false);
 770     FLAG_SET_ERGO(StoreCachedCode, false);
 771     FLAG_SET_ERGO(LoadCachedCode, false);
 772   }
 773 }
 774 
 775 // Ergo set-up of various flags used by the experimental workflow that uses -XX:CacheDataStore. This workflow
 776 // is deprecated and will be removed from Leyden.
 777 bool CDSConfig::setup_experimental_leyden_workflow(bool xshare_auto_cmd_line) {
 778   // Leyden temp work-around:
 779   //
 780   // By default, when using CacheDataStore, use the HeapBasedNarrowOop mode so that
 781   // AOT code can be always work regardless of runtime heap range.
 782   //
 783   // If you are *absolutely sure* that the CompressedOops::mode() will be the same
 784   // between training and production runs (e.g., if you specify -Xmx128m
 785   // for both training and production runs, and you know the OS will always reserve
 786   // the heap under 4GB), you can explicitly disable this with:
 787   //     java -XX:-UseCompatibleCompressedOops -XX:CacheDataStore=...
 788   // However, this is risky and there's a chance that the production run will be slower
 789   // because it is unable to load the AOT code cache.
 790 #ifdef _LP64
 791   // FLAG_SET_ERGO_IF_DEFAULT(UseCompatibleCompressedOops, true); // FIXME @iklam - merge with mainline - UseCompatibleCompressedOops
 792 #endif
 793 
 794   if (FLAG_IS_DEFAULT(AOTClassLinking)) {
 795     FLAG_SET_ERGO(AOTClassLinking, true);
 796   }
 797 
 798   if (SharedArchiveFile != nullptr) {
 799     vm_exit_during_initialization("CacheDataStore and SharedArchiveFile cannot be both specified");
 800   }
 801   if (!AOTClassLinking) {
 802     vm_exit_during_initialization("CacheDataStore requires AOTClassLinking");
 803   }
 804 
 805   if (CDSPreimage == nullptr) {
 806     if (os::file_exists(CacheDataStore) /* && TODO: Need to check if CDS file is valid*/) {
 807       // The CacheDataStore is already up to date. Use it. Also turn on cached code by default.
 808       FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
 809       FLAG_SET_ERGO_IF_DEFAULT(LoadCachedCode, true);
 810 
 811       // Leyden temp: make sure the user knows if CDS archive somehow fails to load.
 812       if (UseSharedSpaces && !xshare_auto_cmd_line) {
 813         log_info(cds)("Enabled -Xshare:on by default for troubleshooting Leyden prototype");
 814         RequireSharedSpaces = true;
 815       }
 816     } else {
 817       // The preimage dumping phase -- run the app and write the preimage file
 818       size_t len = strlen(CacheDataStore) + 10;
 819       char* preimage = AllocateHeap(len, mtArguments);
 820       jio_snprintf(preimage, len, "%s.preimage", CacheDataStore);
 821 
 822       UseSharedSpaces = false;
 823       enable_dumping_static_archive();
 824       CDSPreimage = preimage;
 825       log_info(cds)("CacheDataStore needs to be updated. Writing %s file", CDSPreimage);
 826 
 827       // At VM exit, the module graph may be contaminated with program states. We should rebuild the
 828       // module graph when dumping the CDS final image.
 829       log_info(cds)("full module graph: disabled when writing CDS preimage");
 830       disable_heap_dumping();
 831       stop_dumping_full_module_graph();
 832       FLAG_SET_ERGO(ArchivePackages, false);
 833       FLAG_SET_ERGO(ArchiveProtectionDomains, false);
 834       FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
 835       _is_dumping_static_archive = true;
 836       _is_dumping_preimage_static_archive = true;
 837     }
 838   } else {
 839     // The final image dumping phase -- load the preimage and write the final image file
 840     UseSharedSpaces = true;
 841     log_info(cds)("Generate CacheDataStore %s from CDSPreimage %s", CacheDataStore, CDSPreimage);
 842     // Force -Xbatch for AOT compilation.
 843     if (FLAG_SET_CMDLINE(BackgroundCompilation, false) != JVMFlag::SUCCESS) {
 844       return false;
 845     }
 846     AOTRecordTraining = false; // This will be updated inside MetaspaceShared::preload_and_dump()
 847 
 848     FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
 849     // Settings for AOT
 850     FLAG_SET_ERGO_IF_DEFAULT(StoreCachedCode, true);
 851     if (StoreCachedCode) {
 852       // Cannot dump cached code until metadata and heap are dumped.
 853       disable_dumping_cached_code();
 854     }
 855     _is_dumping_static_archive = true;
 856     _is_dumping_final_static_archive = true;
 857   }
 858 
 859   return true;
 860 }
 861 
 862 void CDSConfig::prepare_for_dumping() {
 863   assert(CDSConfig::is_dumping_archive(), "sanity");
 864 
 865   if (is_dumping_dynamic_archive() && !is_using_archive()) {
 866     assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives");
 867 
 868     // This could happen if SharedArchiveFile has failed to load:
 869     // - -Xshare:off was specified
 870     // - SharedArchiveFile points to an non-existent file.
 871     // - SharedArchiveFile points to an archive that has failed CRC check
 872     // - SharedArchiveFile is not specified and the VM doesn't have a compatible default archive
 873 
 874 #define __THEMSG " is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."
 875     if (RecordDynamicDumpInfo) {
 876       log_error(cds)("-XX:+RecordDynamicDumpInfo%s", __THEMSG);
 877       MetaspaceShared::unrecoverable_loading_error();
 878     } else {

 880       log_warning(cds)("-XX:ArchiveClassesAtExit" __THEMSG);
 881     }
 882 #undef __THEMSG
 883     disable_dumping_dynamic_archive();
 884     return;
 885   }
 886 
 887   check_unsupported_dumping_module_options();
 888 }
 889 
 890 bool CDSConfig::is_dumping_classic_static_archive() {
 891   return _is_dumping_static_archive &&
 892     !is_dumping_preimage_static_archive() &&
 893     !is_dumping_final_static_archive();
 894 }
 895 
 896 bool CDSConfig::is_dumping_preimage_static_archive() {
 897   return _is_dumping_preimage_static_archive;
 898 }
 899 
 900 bool CDSConfig::is_dumping_preimage_static_archive_with_triggers() {
 901   return (!FLAG_IS_DEFAULT(AOTEndTrainingOnMethodEntry)) && is_dumping_preimage_static_archive();
 902 }
 903 
 904 bool CDSConfig::is_dumping_final_static_archive() {
 905   return _is_dumping_final_static_archive;
 906 }
 907 
 908 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) {
 909   _is_dumping_dynamic_archive = true;
 910   if (output_path == nullptr) {
 911     // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo
 912     // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual
 913     // output path.
 914     _output_archive_path = nullptr;
 915   } else {
 916     _output_archive_path = os::strdup_check_oom(output_path, mtArguments);
 917   }
 918 }
 919 
 920 bool CDSConfig::allow_only_single_java_thread() {
 921   // See comments in JVM_StartThread()
 922   return is_dumping_classic_static_archive() || is_dumping_final_static_archive();
 923 }
 924 
 925 bool CDSConfig::is_logging_dynamic_proxies() {
 926   return ClassListWriter::is_enabled() || is_dumping_preimage_static_archive();
 927 }
 928 
 929 // Preserve all states that were examined used during dumptime verification, such
 930 // that the verification result (pass or fail) cannot be changed at runtime.
 931 //
 932 // For example, if the verification of ik requires that class A must be a subtype of B,
 933 // then this relationship between A and B cannot be changed at runtime. I.e., the app
 934 // cannot load alternative versions of A and B such that A is not a subtype of B.
 935 bool CDSConfig::preserve_all_dumptime_verification_states(const InstanceKlass* ik) {
 936   return is_dumping_aot_linked_classes() && SystemDictionaryShared::is_builtin(ik);
 937 }
 938 
 939 bool CDSConfig::is_using_archive() {
 940   return UseSharedSpaces;
 941 }
 942 
 943 bool CDSConfig::is_logging_lambda_form_invokers() {
 944   return ClassListWriter::is_enabled() || is_dumping_dynamic_archive() || is_dumping_preimage_static_archive();
 945 }
 946 
 947 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
 948   if (is_dumping_final_static_archive()) {
 949     // No need to regenerate -- the lambda form invokers should have been regenerated
 950     // in the preimage archive (if allowed)
 951     return false;
 952   } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) {
 953     // The base archive has aot-linked classes that may have AOT-resolved CP references
 954     // that point to the lambda form invokers in the base archive. Such pointers will
 955     // be invalid if lambda form invokers are regenerated in the dynamic archive.
 956     return false;
 957   } else if (CDSConfig::is_dumping_method_handles()) {
 958     // Work around JDK-8310831, as some methods in lambda form holder classes may not get generated.
 959     return false;
 960   } else {
 961     return is_dumping_archive();
 962   }
 963 }
 964 

1095     _is_using_full_module_graph = false;
1096     if (reason != nullptr) {
1097       log_info(cds)("full module graph cannot be loaded: %s", reason);
1098     }
1099   }
1100 }
1101 
1102 bool CDSConfig::is_dumping_aot_linked_classes() {
1103   if (is_dumping_preimage_static_archive()) {
1104     return false;
1105   } else if (is_dumping_dynamic_archive()) {
1106     return is_using_full_module_graph() && AOTClassLinking;
1107   } else if (is_dumping_static_archive()) {
1108     return is_dumping_full_module_graph() && AOTClassLinking;
1109   } else {
1110     return false;
1111   }
1112 }
1113 
1114 bool CDSConfig::is_using_aot_linked_classes() {
1115   if (is_dumping_final_static_archive()) {
1116     // We assume that the final image is being dumped with the exact same module graph as the training run,
1117     // so all aot-linked classes can be loaded.
1118     return _has_aot_linked_classes;
1119   }
1120   // Make sure we have the exact same module graph as in the assembly phase, or else
1121   // some aot-linked classes may not be visible so cannot be loaded.
1122   return is_using_full_module_graph() && _has_aot_linked_classes;
1123 }
1124 
1125 bool CDSConfig::is_dumping_dynamic_proxies() {
1126   return is_dumping_full_module_graph() && is_dumping_invokedynamic() && ArchiveDynamicProxies;
1127 }
1128 
1129 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) {
1130   _has_aot_linked_classes |= has_aot_linked_classes;
1131 }
1132 
1133 bool CDSConfig::is_initing_classes_at_dump_time() {
1134   return is_dumping_heap() && is_dumping_aot_linked_classes();
1135 }
1136 
1137 bool CDSConfig::is_dumping_invokedynamic() {
1138   // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap
1139   // objects used by the archive indy callsites may be replaced at runtime.
1140   return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap();
1141 }
1142 
1143 bool CDSConfig::is_dumping_packages() {
1144   return ArchivePackages && is_dumping_heap();
1145 }
1146 
1147 bool CDSConfig::is_loading_packages() {
1148   return UseSharedSpaces && is_using_full_module_graph() && _is_loading_packages;
1149 }
1150 
1151 bool CDSConfig::is_dumping_protection_domains() {
1152   if (_is_security_manager_allowed) {
1153     // For sanity, don't archive PDs. TODO: can this be relaxed?
1154     return false;
1155   }
1156   // Archived PDs for the modules will reference their java.lang.Module, which must
1157   // also be archived.
1158   return ArchiveProtectionDomains && is_dumping_full_module_graph();
1159 }
1160 
1161 bool CDSConfig::is_loading_protection_domains() {
1162   if (_is_security_manager_allowed) {
1163     // For sanity, don't used any archived PDs. TODO: can this be relaxed?
1164     return false;
1165   }
1166   return UseSharedSpaces && is_using_full_module_graph() && _is_loading_protection_domains;
1167 }
1168 
1169 bool CDSConfig::is_dumping_reflection_data() {
1170   // reflection data use LambdaForm classes
1171   return ArchiveReflectionData && is_dumping_invokedynamic();
1172 }
1173 
1174 // When we are dumping aot-linked classes and we are able to write archived heap objects, we automatically
1175 // enable the archiving of MethodHandles. This will in turn enable the archiving of MethodTypes and hidden
1176 // classes that are used in the implementation of MethodHandles.
1177 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
1178 // and dynamic proxies.
1179 bool CDSConfig::is_dumping_method_handles() {
1180   return is_initing_classes_at_dump_time();
1181 }
1182 
1183 #endif // INCLUDE_CDS_JAVA_HEAP
1184 
1185 // This is allowed by default. We disable it only in the final image dump before the
1186 // metadata and heap are dumped.
1187 static bool _is_dumping_cached_code = true;
1188 
1189 bool CDSConfig::is_dumping_cached_code() {
1190   return _is_dumping_cached_code;
1191 }
1192 
1193 void CDSConfig::disable_dumping_cached_code() {
1194   _is_dumping_cached_code = false;
1195 }
1196 
1197 void CDSConfig::enable_dumping_cached_code() {
1198   _is_dumping_cached_code = true;
1199 }
1200 
1201 bool CDSConfig::is_dumping_adapters() {
1202   return (ArchiveAdapters && is_dumping_final_static_archive());
1203 }
1204 
1205 bool CDSConfig::is_experimental_leyden_workflow() {
1206   return CacheDataStore != nullptr || CDSPreimage != nullptr;
1207 }
< prev index next >