< 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) && AOTAdapterCaching) {
 406     log_debug(aot,codecache,init)("AOTCache is not specified - AOTAdapterCaching is ignored");










 407   }
 408 
 409   if (FLAG_IS_DEFAULT(AOTCache) && FLAG_IS_DEFAULT(AOTConfiguration) && FLAG_IS_DEFAULT(AOTMode)) {
 410     // AOTCache/AOTConfiguration/AOTMode not used.
 411     return;
 412   } else {
 413     _new_aot_flags_used = true;
 414   }
 415 
 416   if (FLAG_IS_DEFAULT(AOTMode) || strcmp(AOTMode, "auto") == 0 || strcmp(AOTMode, "on") == 0) {
 417     check_aotmode_auto_or_on();
 418   } else if (strcmp(AOTMode, "off") == 0) {
 419     check_aotmode_off();


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




















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














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















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












 515   if (AOTClassLinking) {
 516     // If AOTClassLinking is specified, enable all AOT optimizations by default.
 517     FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true);





 518   } else {
 519     // AOTInvokeDynamicLinking depends on AOTClassLinking.
 520     FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);











 521   }
 522 





 523   if (is_dumping_static_archive()) {
 524     if (is_dumping_preimage_static_archive() || is_dumping_final_static_archive()) {
 525       // Don't tweak execution mode
 526     } else if (!mode_flag_cmd_line) {
 527       // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
 528       //
 529       // If your classlist is large and you don't care about deterministic dumping, you can use
 530       // -Xshare:dump -Xmixed to improve dumping speed.
 531       Arguments::set_mode_flags(Arguments::_int);
 532     } else if (Arguments::mode() == Arguments::_comp) {
 533       // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
 534       // Java code, so there's not much benefit in running -Xcomp.
 535       log_info(cds)("reduced -Xcomp to -Xmixed for static dumping");
 536       Arguments::set_mode_flags(Arguments::_mixed);
 537     }
 538 
 539     // String deduplication may cause CDS to iterate the strings in different order from one
 540     // run to another which resulting in non-determinstic CDS archives.
 541     // Disable UseStringDeduplication while dumping CDS archive.
 542     UseStringDeduplication = false;

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





































































































































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

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




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














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

 807 }
 808 
 809 bool CDSConfig::is_dumping_aot_linked_classes() {
 810   if (is_dumping_preimage_static_archive()) {
 811     return false;
 812   } else if (is_dumping_dynamic_archive()) {
 813     return is_using_full_module_graph() && AOTClassLinking;
 814   } else if (is_dumping_static_archive()) {
 815     return is_dumping_full_module_graph() && AOTClassLinking;
 816   } else {
 817     return false;
 818   }
 819 }
 820 
 821 bool CDSConfig::is_using_aot_linked_classes() {
 822   // Make sure we have the exact same module graph as in the assembly phase, or else
 823   // some aot-linked classes may not be visible so cannot be loaded.
 824   return is_using_full_module_graph() && _has_aot_linked_classes;
 825 }
 826 




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































 841 // When we are dumping aot-linked classes and we are able to write archived heap objects, we automatically
 842 // enable the archiving of MethodHandles. This will in turn enable the archiving of MethodTypes and hidden
 843 // classes that are used in the implementation of MethodHandles.
 844 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
 845 // and dynamic proxies.
 846 bool CDSConfig::is_dumping_method_handles() {
 847   return is_initing_classes_at_dump_time();
 848 }
 849 
 850 #endif // INCLUDE_CDS_JAVA_HEAP
 851 
 852 // AOT code generation and its archiving is disabled by default.
 853 // We enable it only in the final image dump after the metadata and heap are dumped.
 854 // This affects only JITed code because it may have embedded oops and metadata pointers
 855 // which AOT code encodes as offsets in final CDS archive regions.
 856 
 857 static bool _is_dumping_aot_code = false;
 858 
 859 bool CDSConfig::is_dumping_aot_code() {
 860   return _is_dumping_aot_code;
 861 }
 862 
 863 void CDSConfig::disable_dumping_aot_code() {
 864   _is_dumping_aot_code = false;
 865 }
 866 
 867 void CDSConfig::enable_dumping_aot_code() {
 868   _is_dumping_aot_code = true;
 869 }





   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/aotCodeCache.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       AOTCodeCache::disable_caching();
 647     }
 648   }
 649 
 650 #ifdef _WINDOWS
 651   // This optimization is not working on Windows for some reason. See JDK-8338604.
 652   FLAG_SET_ERGO(ArchiveReflectionData, false);
 653 #endif
 654 
 655   if (is_dumping_static_archive()) {
 656     if (is_dumping_preimage_static_archive() || is_dumping_final_static_archive()) {
 657       // Don't tweak execution mode
 658     } else if (!mode_flag_cmd_line) {
 659       // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
 660       //
 661       // If your classlist is large and you don't care about deterministic dumping, you can use
 662       // -Xshare:dump -Xmixed to improve dumping speed.
 663       Arguments::set_mode_flags(Arguments::_int);
 664     } else if (Arguments::mode() == Arguments::_comp) {
 665       // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
 666       // Java code, so there's not much benefit in running -Xcomp.
 667       log_info(cds)("reduced -Xcomp to -Xmixed for static dumping");
 668       Arguments::set_mode_flags(Arguments::_mixed);
 669     }
 670 
 671     // String deduplication may cause CDS to iterate the strings in different order from one
 672     // run to another which resulting in non-determinstic CDS archives.
 673     // Disable UseStringDeduplication while dumping CDS archive.
 674     UseStringDeduplication = false;

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

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

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