7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "cds/aotLogging.hpp"
26 #include "cds/aotMapLogger.hpp"
27 #include "cds/archiveHeapLoader.hpp"
28 #include "cds/cdsConfig.hpp"
29 #include "cds/classListWriter.hpp"
30 #include "cds/filemap.hpp"
31 #include "cds/heapShared.hpp"
32 #include "classfile/classLoaderDataShared.hpp"
33 #include "classfile/moduleEntry.hpp"
34 #include "code/aotCodeCache.hpp"
35 #include "include/jvm_io.h"
36 #include "logging/log.hpp"
37 #include "memory/universe.hpp"
38 #include "prims/jvmtiAgentList.hpp"
39 #include "runtime/arguments.hpp"
40 #include "runtime/globals_extension.hpp"
41 #include "runtime/java.hpp"
42 #include "runtime/vmThread.hpp"
43 #include "utilities/defaultStream.hpp"
44 #include "utilities/formatBuffer.hpp"
45
46 bool CDSConfig::_is_dumping_static_archive = false;
47 bool CDSConfig::_is_dumping_preimage_static_archive = false;
48 bool CDSConfig::_is_dumping_final_static_archive = false;
49 bool CDSConfig::_is_dumping_dynamic_archive = false;
50 bool CDSConfig::_is_using_optimized_module_handling = true;
51 bool CDSConfig::_is_dumping_full_module_graph = true;
52 bool CDSConfig::_is_using_full_module_graph = true;
53 bool CDSConfig::_has_aot_linked_classes = false;
54 bool CDSConfig::_is_single_command_training = false;
55 bool CDSConfig::_has_temp_aot_config_file = false;
56 bool CDSConfig::_old_cds_flags_used = false;
57 bool CDSConfig::_new_aot_flags_used = false;
58 bool CDSConfig::_disable_heap_dumping = false;
59
60 const char* CDSConfig::_default_archive_path = nullptr;
61 const char* CDSConfig::_input_static_archive_path = nullptr;
62 const char* CDSConfig::_input_dynamic_archive_path = nullptr;
63 const char* CDSConfig::_output_archive_path = nullptr;
64
65 JavaThread* CDSConfig::_dumper_thread = nullptr;
66
67 int CDSConfig::get_status() {
68 assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized");
69 return (is_dumping_archive() ? IS_DUMPING_ARCHIVE : 0) |
70 (is_dumping_method_handles() ? IS_DUMPING_METHOD_HANDLES : 0) |
71 (is_dumping_static_archive() ? IS_DUMPING_STATIC_ARCHIVE : 0) |
72 (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) |
73 (is_using_archive() ? IS_USING_ARCHIVE : 0);
74 }
75
76 DEBUG_ONLY(static bool _cds_ergo_initialize_started = false);
77
78 void CDSConfig::ergo_initialize() {
79 DEBUG_ONLY(_cds_ergo_initialize_started = true);
80
81 if (is_dumping_static_archive() && !is_dumping_final_static_archive()) {
82 // Note: -Xshare and -XX:AOTMode flags are mutually exclusive.
83 // - Classic workflow: -Xshare:on and -Xshare:dump cannot take effect at the same time.
84 // - JEP 483 workflow: -XX:AOTMode:record and -XX:AOTMode=on cannot take effect at the same time.
85 // So we can never come to here with RequireSharedSpaces==true.
86 assert(!RequireSharedSpaces, "sanity");
87
88 // If dumping the classic archive, or making an AOT training run (dumping a preimage archive),
89 // for sanity, parse all classes from classfiles.
90 // TODO: in the future, if we want to support re-training on top of an existing AOT cache, this
91 // needs to be changed.
92 UseSharedSpaces = false;
93 }
94
95 // Initialize shared archive paths which could include both base and dynamic archive paths
96 // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly.
97 if (is_dumping_static_archive() || is_using_archive()) {
98 if (new_aot_flags_used()) {
99 ergo_init_aot_paths();
100 } else {
101 ergo_init_classic_archive_paths();
102 }
103 }
104
105 if (!is_dumping_heap()) {
106 _is_dumping_full_module_graph = false;
107 }
108
109 AOTMapLogger::ergo_initialize();
110 }
111
112 const char* CDSConfig::default_archive_path() {
113 // The path depends on UseCompressedOops, etc, which are set by GC ergonomics just
114 // before CDSConfig::ergo_initialize() is called.
115 assert(_cds_ergo_initialize_started, "sanity");
116 if (_default_archive_path == nullptr) {
117 stringStream tmp;
118 if (is_vm_statically_linked()) {
119 // It's easier to form the path using JAVA_HOME as os::jvm_path
120 // gives the path to the launcher executable on static JDK.
121 const char* subdir = WINDOWS_ONLY("bin") NOT_WINDOWS("lib");
122 tmp.print("%s%s%s%s%s%sclasses",
123 Arguments::get_java_home(), os::file_separator(),
124 subdir, os::file_separator(),
125 Abstract_VM_Version::vm_variant(), os::file_separator());
126 } else {
127 // Assume .jsa is in the same directory where libjvm resides on
128 // non-static JDK.
129 char jvm_path[JVM_MAXPATHLEN];
279 // Check for case (c)
280 if (RecordDynamicDumpInfo) {
281 vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
282 SharedArchiveFile);
283 }
284 if (ArchiveClassesAtExit != nullptr) {
285 vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
286 SharedArchiveFile);
287 }
288 }
289
290 if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) {
291 vm_exit_during_initialization(
292 "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit",
293 SharedArchiveFile);
294 }
295 }
296 }
297 }
298
299 void CDSConfig::check_internal_module_property(const char* key, const char* value) {
300 if (Arguments::is_incompatible_cds_internal_module_property(key)) {
301 stop_using_optimized_module_handling();
302 aot_log_info(aot)("optimized module handling: disabled due to incompatible property: %s=%s", key, value);
303 }
304 }
305
306 void CDSConfig::check_incompatible_property(const char* key, const char* value) {
307 static const char* incompatible_properties[] = {
308 "java.system.class.loader",
309 "jdk.module.showModuleResolution",
310 "jdk.module.validation"
311 };
312
313 for (const char* property : incompatible_properties) {
314 if (strcmp(key, property) == 0) {
315 stop_dumping_full_module_graph();
316 stop_using_full_module_graph();
317 aot_log_info(aot)("full module graph: disabled due to incompatible property: %s=%s", key, value);
318 break;
319 }
320 }
321
322 }
323
324 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS.
325 static const char* find_any_unsupported_module_option() {
326 // Note that arguments.cpp has translated the command-line options into properties. If we find an
327 // unsupported property, translate it back to its command-line option for better error reporting.
328
329 // The following properties are checked by Arguments::is_internal_module_property() and cannot be
330 // directly specified in the command-line.
331 static const char* unsupported_module_properties[] = {
332 "jdk.module.limitmods",
333 "jdk.module.upgrade.path",
334 "jdk.module.patch.0"
335 };
336 static const char* unsupported_module_options[] = {
337 "--limit-modules",
338 "--upgrade-module-path",
339 "--patch-module"
340 };
341
621 _input_static_archive_path = default_archive_path();
622 } else {
623 _input_static_archive_path = AOTCache;
624 }
625 }
626 }
627
628 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line) {
629 assert(!_cds_ergo_initialize_started, "This is called earlier than CDSConfig::ergo_initialize()");
630
631 check_aot_flags();
632
633 if (!FLAG_IS_DEFAULT(AOTMode)) {
634 // Using any form of the new AOTMode switch enables enhanced optimizations.
635 FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true);
636 }
637
638 setup_compiler_args();
639
640 if (AOTClassLinking) {
641 // If AOTClassLinking is specified, enable all AOT optimizations by default.
642 FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true);
643 } else {
644 // AOTInvokeDynamicLinking depends on AOTClassLinking.
645 FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);
646 }
647
648 if (is_dumping_static_archive()) {
649 if (is_dumping_preimage_static_archive()) {
650 // Don't tweak execution mode during AOT training run
651 } else if (is_dumping_final_static_archive()) {
652 if (Arguments::mode() == Arguments::_comp) {
653 // AOT assembly phase submits the non-blocking compilation requests
654 // for methods collected during training run, then waits for all compilations
655 // to complete. With -Xcomp, we block for each compilation request, which is
656 // counter-productive. Switching back to mixed mode improves testing time
657 // with AOT and -Xcomp.
658 Arguments::set_mode_flags(Arguments::_mixed);
659 }
660 } else if (!mode_flag_cmd_line) {
661 // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
662 //
663 // If your classlist is large and you don't care about deterministic dumping, you can use
664 // -Xshare:dump -Xmixed to improve dumping speed.
665 Arguments::set_mode_flags(Arguments::_int);
666 } else if (Arguments::mode() == Arguments::_comp) {
667 // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
705 }
706 if (is_using_archive() && has_unsupported_runtime_module_options()) {
707 UseSharedSpaces = false;
708 }
709
710 if (is_dumping_archive()) {
711 // Always verify non-system classes during CDS dump
712 if (!BytecodeVerificationRemote) {
713 BytecodeVerificationRemote = true;
714 aot_log_info(aot)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
715 }
716 }
717
718 if (is_dumping_classic_static_archive() && AOTClassLinking) {
719 if (JvmtiAgentList::disable_agent_list()) {
720 FLAG_SET_ERGO(AllowArchivingWithJavaAgent, false);
721 log_warning(cds)("Disabled all JVMTI agents with -Xshare:dump -XX:+AOTClassLinking");
722 }
723 }
724
725 return true;
726 }
727
728 void CDSConfig::setup_compiler_args() {
729 // AOT profiles and AOT-compiled code are supported only in the JEP 483 workflow.
730 bool can_dump_profile_and_compiled_code = AOTClassLinking && new_aot_flags_used();
731
732 if (is_dumping_preimage_static_archive() && can_dump_profile_and_compiled_code) {
733 // JEP 483 workflow -- training
734 FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
735 FLAG_SET_ERGO(AOTReplayTraining, false);
736 AOTCodeCache::disable_caching(); // No AOT code generation during training run
737 } else if (is_dumping_final_static_archive() && can_dump_profile_and_compiled_code) {
738 // JEP 483 workflow -- assembly
739 FLAG_SET_ERGO(AOTRecordTraining, false);
740 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
741 AOTCodeCache::enable_caching(); // Generate AOT code during assembly phase.
742 disable_dumping_aot_code(); // Don't dump AOT code until metadata and heap are dumped.
743 } else if (is_using_archive() && new_aot_flags_used()) {
744 // JEP 483 workflow -- production
745 FLAG_SET_ERGO(AOTRecordTraining, false);
746 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
747 AOTCodeCache::enable_caching();
748 } else {
749 FLAG_SET_ERGO(AOTReplayTraining, false);
750 FLAG_SET_ERGO(AOTRecordTraining, false);
751 AOTCodeCache::disable_caching();
752 }
753 }
754
755 void CDSConfig::prepare_for_dumping() {
756 assert(CDSConfig::is_dumping_archive(), "sanity");
757
758 if (is_dumping_dynamic_archive() && !is_using_archive()) {
759 assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives");
760
761 // This could happen if SharedArchiveFile has failed to load:
762 // - -Xshare:off was specified
763 // - SharedArchiveFile points to an non-existent file.
764 // - SharedArchiveFile points to an archive that has failed CRC check
765 // - SharedArchiveFile is not specified and the VM doesn't have a compatible default archive
766
767 #define __THEMSG " is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."
768 if (RecordDynamicDumpInfo) {
769 aot_log_error(aot)("-XX:+RecordDynamicDumpInfo%s", __THEMSG);
770 AOTMetaspace::unrecoverable_loading_error();
771 } else {
773 aot_log_warning(aot)("-XX:ArchiveClassesAtExit" __THEMSG);
774 }
775 #undef __THEMSG
776 disable_dumping_dynamic_archive();
777 return;
778 }
779
780 check_unsupported_dumping_module_options();
781 }
782
783 bool CDSConfig::is_dumping_classic_static_archive() {
784 return _is_dumping_static_archive &&
785 !is_dumping_preimage_static_archive() &&
786 !is_dumping_final_static_archive();
787 }
788
789 bool CDSConfig::is_dumping_preimage_static_archive() {
790 return _is_dumping_preimage_static_archive;
791 }
792
793 bool CDSConfig::is_dumping_final_static_archive() {
794 return _is_dumping_final_static_archive;
795 }
796
797 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) {
798 _is_dumping_dynamic_archive = true;
799 if (output_path == nullptr) {
800 // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo
801 // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual
802 // output path.
803 _output_archive_path = nullptr;
804 } else {
805 _output_archive_path = os::strdup_check_oom(output_path, mtArguments);
806 }
807 }
808
809 bool CDSConfig::allow_only_single_java_thread() {
810 // See comments in JVM_StartThread()
811 return is_dumping_classic_static_archive() || is_dumping_final_static_archive();
812 }
813
814 bool CDSConfig::is_using_archive() {
815 return UseSharedSpaces;
816 }
817
818 bool CDSConfig::is_using_only_default_archive() {
819 return is_using_archive() &&
820 input_static_archive_path() != nullptr &&
821 default_archive_path() != nullptr &&
822 strcmp(input_static_archive_path(), default_archive_path()) == 0 &&
823 input_dynamic_archive_path() == nullptr;
824 }
825
826 bool CDSConfig::is_logging_lambda_form_invokers() {
827 return ClassListWriter::is_enabled() || is_dumping_dynamic_archive();
828 }
829
830 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
831 if (is_dumping_final_static_archive()) {
832 // No need to regenerate -- the lambda form invokers should have been regenerated
833 // in the preimage archive (if allowed)
834 return false;
835 } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) {
836 // The base archive has aot-linked classes that may have AOT-resolved CP references
837 // that point to the lambda form invokers in the base archive. Such pointers will
838 // be invalid if lambda form invokers are regenerated in the dynamic archive.
839 return false;
840 } else {
841 return is_dumping_archive();
842 }
843 }
844
845 void CDSConfig::stop_using_optimized_module_handling() {
846 _is_using_optimized_module_handling = false;
847 _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling()
980 }
981
982 bool CDSConfig::is_dumping_aot_linked_classes() {
983 if (is_dumping_preimage_static_archive()) {
984 return false;
985 } else if (is_dumping_dynamic_archive()) {
986 return is_using_full_module_graph() && AOTClassLinking;
987 } else if (is_dumping_static_archive()) {
988 return is_dumping_full_module_graph() && AOTClassLinking;
989 } else {
990 return false;
991 }
992 }
993
994 bool CDSConfig::is_using_aot_linked_classes() {
995 // Make sure we have the exact same module graph as in the assembly phase, or else
996 // some aot-linked classes may not be visible so cannot be loaded.
997 return is_using_full_module_graph() && _has_aot_linked_classes;
998 }
999
1000 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) {
1001 _has_aot_linked_classes |= has_aot_linked_classes;
1002 }
1003
1004 bool CDSConfig::is_initing_classes_at_dump_time() {
1005 return is_dumping_heap() && is_dumping_aot_linked_classes();
1006 }
1007
1008 bool CDSConfig::is_dumping_invokedynamic() {
1009 // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap
1010 // objects used by the archive indy callsites may be replaced at runtime.
1011 return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap();
1012 }
1013
1014 // When we are dumping aot-linked classes and we are able to write archived heap objects, we automatically
1015 // enable the archiving of MethodHandles. This will in turn enable the archiving of MethodTypes and hidden
1016 // classes that are used in the implementation of MethodHandles.
1017 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
1018 // and dynamic proxies.
1019 bool CDSConfig::is_dumping_method_handles() {
1020 return is_initing_classes_at_dump_time();
1021 }
1022
1023 #endif // INCLUDE_CDS_JAVA_HEAP
1024
1025 // AOT code generation and its archiving is disabled by default.
1026 // We enable it only in the final image dump after the metadata and heap are dumped.
1027 // This affects only JITed code because it may have embedded oops and metadata pointers
1028 // which AOT code encodes as offsets in final CDS archive regions.
1029
1030 static bool _is_dumping_aot_code = false;
1031
1032 bool CDSConfig::is_dumping_aot_code() {
1033 return _is_dumping_aot_code;
|
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "cds/aotLogging.hpp"
26 #include "cds/aotMapLogger.hpp"
27 #include "cds/aotMetaspace.hpp"
28 #include "cds/archiveHeapLoader.hpp"
29 #include "cds/cds_globals.hpp"
30 #include "cds/cdsConfig.hpp"
31 #include "cds/classListWriter.hpp"
32 #include "cds/filemap.hpp"
33 #include "cds/heapShared.hpp"
34 #include "classfile/classLoaderDataShared.hpp"
35 #include "classfile/moduleEntry.hpp"
36 #include "classfile/systemDictionaryShared.hpp"
37 #include "code/aotCodeCache.hpp"
38 #include "compiler/compilerDefinitions.inline.hpp"
39 #include "include/jvm_io.h"
40 #include "logging/log.hpp"
41 #include "memory/universe.hpp"
42 #include "prims/jvmtiAgentList.hpp"
43 #include "prims/jvmtiExport.hpp"
44 #include "runtime/arguments.hpp"
45 #include "runtime/globals_extension.hpp"
46 #include "runtime/java.hpp"
47 #include "runtime/vmThread.hpp"
48 #include "utilities/defaultStream.hpp"
49 #include "utilities/formatBuffer.hpp"
50
51 bool CDSConfig::_is_dumping_static_archive = false;
52 bool CDSConfig::_is_dumping_preimage_static_archive = false;
53 bool CDSConfig::_is_dumping_final_static_archive = false;
54 bool CDSConfig::_is_dumping_dynamic_archive = false;
55 bool CDSConfig::_is_using_optimized_module_handling = true;
56 bool CDSConfig::_is_dumping_full_module_graph = true;
57 bool CDSConfig::_is_using_full_module_graph = true;
58 bool CDSConfig::_has_aot_linked_classes = false;
59 bool CDSConfig::_is_single_command_training = false;
60 bool CDSConfig::_has_temp_aot_config_file = false;
61 bool CDSConfig::_is_loading_packages = false;
62 bool CDSConfig::_is_loading_protection_domains = false;
63 bool CDSConfig::_is_security_manager_allowed = false;
64 bool CDSConfig::_old_cds_flags_used = false;
65 bool CDSConfig::_new_aot_flags_used = false;
66 bool CDSConfig::_disable_heap_dumping = false;
67
68 const char* CDSConfig::_default_archive_path = nullptr;
69 const char* CDSConfig::_input_static_archive_path = nullptr;
70 const char* CDSConfig::_input_dynamic_archive_path = nullptr;
71 const char* CDSConfig::_output_archive_path = nullptr;
72
73 JavaThread* CDSConfig::_dumper_thread = nullptr;
74
75 int CDSConfig::get_status() {
76 assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized");
77 return (is_dumping_archive() ? IS_DUMPING_ARCHIVE : 0) |
78 (is_dumping_method_handles() ? IS_DUMPING_METHOD_HANDLES : 0) |
79 (is_dumping_static_archive() ? IS_DUMPING_STATIC_ARCHIVE : 0) |
80 (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) |
81 (is_using_archive() ? IS_USING_ARCHIVE : 0) |
82 (is_dumping_heap() ? IS_DUMPING_HEAP : 0) |
83 (is_logging_dynamic_proxies() ? IS_LOGGING_DYNAMIC_PROXIES : 0) |
84 (is_dumping_packages() ? IS_DUMPING_PACKAGES : 0) |
85 (is_dumping_protection_domains() ? IS_DUMPING_PROTECTION_DOMAINS : 0);
86 }
87
88 DEBUG_ONLY(static bool _cds_ergo_initialize_started = false);
89
90 void CDSConfig::ergo_initialize() {
91 DEBUG_ONLY(_cds_ergo_initialize_started = true);
92
93 if (is_dumping_static_archive() && !is_dumping_final_static_archive()) {
94 // Note: -Xshare and -XX:AOTMode flags are mutually exclusive.
95 // - Classic workflow: -Xshare:on and -Xshare:dump cannot take effect at the same time.
96 // - JEP 483 workflow: -XX:AOTMode:record and -XX:AOTMode=on cannot take effect at the same time.
97 // So we can never come to here with RequireSharedSpaces==true.
98 assert(!RequireSharedSpaces, "sanity");
99
100 // If dumping the classic archive, or making an AOT training run (dumping a preimage archive),
101 // for sanity, parse all classes from classfiles.
102 // TODO: in the future, if we want to support re-training on top of an existing AOT cache, this
103 // needs to be changed.
104 UseSharedSpaces = false;
105 }
106
107 // Initialize shared archive paths which could include both base and dynamic archive paths
108 // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly.
109 if (is_dumping_static_archive() || is_using_archive()) {
110 if (new_aot_flags_used()) {
111 ergo_init_aot_paths();
112 } else {
113 ergo_init_classic_archive_paths();
114 }
115 }
116
117 if (!is_dumping_heap()) {
118 _is_dumping_full_module_graph = false;
119 }
120
121 AOTMapLogger::ergo_initialize();
122
123 #ifdef _LP64
124 //
125 // By default, when using AOTClassLinking, use the CompressedOops::HeapBasedNarrowOop
126 // mode so that AOT code can be always work regardless of runtime heap range.
127 //
128 // If you are *absolutely sure* that the CompressedOops::mode() will be the same
129 // between training and production runs (e.g., if you specify -Xmx128m for
130 // both training and production runs, and you know the OS will always reserve
131 // the heap under 4GB), you can explicitly disable this with:
132 // java -XX:+UnlockDiagnosticVMOptions -XX:-UseCompatibleCompressedOops ...
133 // However, this is risky and there's a chance that the production run will be slower
134 // than expected because it is unable to load the AOT code cache.
135 //
136 if (UseCompressedOops && AOTCodeCache::is_caching_enabled()) {
137 FLAG_SET_ERGO_IF_DEFAULT(UseCompatibleCompressedOops, true);
138 } else if (!FLAG_IS_DEFAULT(UseCompatibleCompressedOops)) {
139 FLAG_SET_ERGO(UseCompatibleCompressedOops, false);
140 }
141 #endif // _LP64
142 }
143
144 const char* CDSConfig::default_archive_path() {
145 // The path depends on UseCompressedOops, etc, which are set by GC ergonomics just
146 // before CDSConfig::ergo_initialize() is called.
147 assert(_cds_ergo_initialize_started, "sanity");
148 if (_default_archive_path == nullptr) {
149 stringStream tmp;
150 if (is_vm_statically_linked()) {
151 // It's easier to form the path using JAVA_HOME as os::jvm_path
152 // gives the path to the launcher executable on static JDK.
153 const char* subdir = WINDOWS_ONLY("bin") NOT_WINDOWS("lib");
154 tmp.print("%s%s%s%s%s%sclasses",
155 Arguments::get_java_home(), os::file_separator(),
156 subdir, os::file_separator(),
157 Abstract_VM_Version::vm_variant(), os::file_separator());
158 } else {
159 // Assume .jsa is in the same directory where libjvm resides on
160 // non-static JDK.
161 char jvm_path[JVM_MAXPATHLEN];
311 // Check for case (c)
312 if (RecordDynamicDumpInfo) {
313 vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
314 SharedArchiveFile);
315 }
316 if (ArchiveClassesAtExit != nullptr) {
317 vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
318 SharedArchiveFile);
319 }
320 }
321
322 if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) {
323 vm_exit_during_initialization(
324 "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit",
325 SharedArchiveFile);
326 }
327 }
328 }
329 }
330
331 static char* bad_module_prop_key = nullptr;
332 static char* bad_module_prop_value = nullptr;
333
334 void CDSConfig::check_internal_module_property(const char* key, const char* value) {
335 if (Arguments::is_incompatible_cds_internal_module_property(key)) {
336 stop_using_optimized_module_handling();
337 if (bad_module_prop_key == nullptr) {
338 // We don't want to print an unconditional warning here, as we are still processing the command line.
339 // A later argument may specify something like -Xshare:off, which makes such a warning irrelevant.
340 //
341 // Instead, we save the info so we can warn when necessary: we are doing it only during AOT Cache
342 // creation for now, but could add it to other places.
343 bad_module_prop_key = os::strdup(key);
344 bad_module_prop_value = os::strdup(value);
345 }
346 aot_log_info(aot)("optimized module handling: disabled due to incompatible property: %s=%s", key, value);
347 }
348 }
349
350 void CDSConfig::check_incompatible_property(const char* key, const char* value) {
351 static const char* incompatible_properties[] = {
352 "java.system.class.loader",
353 "jdk.module.showModuleResolution",
354 "jdk.module.validation"
355 };
356
357 for (const char* property : incompatible_properties) {
358 if (strcmp(key, property) == 0) {
359 stop_dumping_full_module_graph();
360 stop_using_full_module_graph();
361 aot_log_info(aot)("full module graph: disabled due to incompatible property: %s=%s", key, value);
362 break;
363 }
364 }
365
366 // Match the logic in java/lang/System.java, but we need to know this before the System class is initialized.
367 if (strcmp(key, "java.security.manager") == 0) {
368 if (strcmp(value, "disallowed") != 0) {
369 _is_security_manager_allowed = true;
370 }
371 }
372 }
373
374 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS.
375 static const char* find_any_unsupported_module_option() {
376 // Note that arguments.cpp has translated the command-line options into properties. If we find an
377 // unsupported property, translate it back to its command-line option for better error reporting.
378
379 // The following properties are checked by Arguments::is_internal_module_property() and cannot be
380 // directly specified in the command-line.
381 static const char* unsupported_module_properties[] = {
382 "jdk.module.limitmods",
383 "jdk.module.upgrade.path",
384 "jdk.module.patch.0"
385 };
386 static const char* unsupported_module_options[] = {
387 "--limit-modules",
388 "--upgrade-module-path",
389 "--patch-module"
390 };
391
671 _input_static_archive_path = default_archive_path();
672 } else {
673 _input_static_archive_path = AOTCache;
674 }
675 }
676 }
677
678 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line) {
679 assert(!_cds_ergo_initialize_started, "This is called earlier than CDSConfig::ergo_initialize()");
680
681 check_aot_flags();
682
683 if (!FLAG_IS_DEFAULT(AOTMode)) {
684 // Using any form of the new AOTMode switch enables enhanced optimizations.
685 FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true);
686 }
687
688 setup_compiler_args();
689
690 if (AOTClassLinking) {
691 // If AOTClassLinking is specified, enable all these optimizations by default.
692 FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true);
693 FLAG_SET_ERGO_IF_DEFAULT(ArchiveDynamicProxies, true);
694 FLAG_SET_ERGO_IF_DEFAULT(ArchiveLoaderLookupCache, true);
695 FLAG_SET_ERGO_IF_DEFAULT(ArchiveReflectionData, true);
696
697 // For simplicity, enable these by default only for new workflow
698 if (new_aot_flags_used()) {
699 // These flags will be removed when JDK-8350550 is merged from mainline
700 FLAG_SET_ERGO_IF_DEFAULT(ArchivePackages, true);
701 FLAG_SET_ERGO_IF_DEFAULT(ArchiveProtectionDomains, true);
702 }
703 } else {
704 // All of these *might* depend on AOTClassLinking. Better be safe than sorry.
705 FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);
706 FLAG_SET_ERGO(ArchiveDynamicProxies, false);
707 FLAG_SET_ERGO(ArchiveLoaderLookupCache, false);
708 FLAG_SET_ERGO(ArchivePackages, false);
709 FLAG_SET_ERGO(ArchiveProtectionDomains, false);
710 FLAG_SET_ERGO(ArchiveReflectionData, false);
711
712 if (CDSConfig::is_dumping_archive()) {
713 FLAG_SET_ERGO(AOTRecordTraining, false);
714 FLAG_SET_ERGO(AOTReplayTraining, false);
715 AOTCodeCache::disable_caching();
716 }
717 }
718 if (is_dumping_static_archive()) {
719 if (is_dumping_preimage_static_archive()) {
720 // Don't tweak execution mode during AOT training run
721 } else if (is_dumping_final_static_archive()) {
722 if (Arguments::mode() == Arguments::_comp) {
723 // AOT assembly phase submits the non-blocking compilation requests
724 // for methods collected during training run, then waits for all compilations
725 // to complete. With -Xcomp, we block for each compilation request, which is
726 // counter-productive. Switching back to mixed mode improves testing time
727 // with AOT and -Xcomp.
728 Arguments::set_mode_flags(Arguments::_mixed);
729 }
730 } else if (!mode_flag_cmd_line) {
731 // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
732 //
733 // If your classlist is large and you don't care about deterministic dumping, you can use
734 // -Xshare:dump -Xmixed to improve dumping speed.
735 Arguments::set_mode_flags(Arguments::_int);
736 } else if (Arguments::mode() == Arguments::_comp) {
737 // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
775 }
776 if (is_using_archive() && has_unsupported_runtime_module_options()) {
777 UseSharedSpaces = false;
778 }
779
780 if (is_dumping_archive()) {
781 // Always verify non-system classes during CDS dump
782 if (!BytecodeVerificationRemote) {
783 BytecodeVerificationRemote = true;
784 aot_log_info(aot)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
785 }
786 }
787
788 if (is_dumping_classic_static_archive() && AOTClassLinking) {
789 if (JvmtiAgentList::disable_agent_list()) {
790 FLAG_SET_ERGO(AllowArchivingWithJavaAgent, false);
791 log_warning(cds)("Disabled all JVMTI agents with -Xshare:dump -XX:+AOTClassLinking");
792 }
793 }
794
795 if (AOTClassLinking) {
796 if (is_dumping_final_static_archive() && !is_dumping_full_module_graph()) {
797 if (bad_module_prop_key != nullptr) {
798 log_warning(cds)("optimized module handling/full module graph: disabled due to incompatible property: %s=%s",
799 bad_module_prop_key, bad_module_prop_value);
800 }
801 vm_exit_during_initialization("AOT cache cannot be created because AOTClassLinking is enabled but full module graph is disabled");
802 }
803 }
804
805 return true;
806 }
807
808 void CDSConfig::setup_compiler_args() {
809 // AOT profiles and AOT-compiled methods are supported only in the JEP 483 workflow.
810 bool can_dump_profile_and_compiled_code = !CompilerConfig::is_interpreter_only() && AOTClassLinking && new_aot_flags_used();
811 bool can_use_profile_and_compiled_code = !CompilerConfig::is_interpreter_only() && new_aot_flags_used();
812
813 if (is_dumping_preimage_static_archive() && can_dump_profile_and_compiled_code) {
814 // JEP 483 workflow -- training
815 FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
816 FLAG_SET_ERGO(AOTReplayTraining, false);
817 AOTCodeCache::disable_caching(); // No AOT code generation during training run
818 FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
819 } else if (is_dumping_final_static_archive() && can_dump_profile_and_compiled_code) {
820 // JEP 483 workflow -- assembly
821 FLAG_SET_ERGO(AOTRecordTraining, false);
822 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
823 AOTCodeCache::enable_caching(); // Generate AOT code during assembly phase.
824 FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
825 disable_dumping_aot_code(); // Don't dump AOT code until metadata and heap are dumped.
826 } else if (is_using_archive() && can_use_profile_and_compiled_code) {
827 // JEP 483 workflow -- production
828 FLAG_SET_ERGO(AOTRecordTraining, false);
829 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
830 AOTCodeCache::enable_caching();
831 FLAG_SET_ERGO_IF_DEFAULT(UseAOTCodeLoadThread, true);
832 } else {
833 FLAG_SET_ERGO(AOTReplayTraining, false);
834 FLAG_SET_ERGO(AOTRecordTraining, false);
835 AOTCodeCache::disable_caching();
836 FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
837 }
838 }
839
840 void CDSConfig::prepare_for_dumping() {
841 assert(CDSConfig::is_dumping_archive(), "sanity");
842
843 if (is_dumping_dynamic_archive() && !is_using_archive()) {
844 assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives");
845
846 // This could happen if SharedArchiveFile has failed to load:
847 // - -Xshare:off was specified
848 // - SharedArchiveFile points to an non-existent file.
849 // - SharedArchiveFile points to an archive that has failed CRC check
850 // - SharedArchiveFile is not specified and the VM doesn't have a compatible default archive
851
852 #define __THEMSG " is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."
853 if (RecordDynamicDumpInfo) {
854 aot_log_error(aot)("-XX:+RecordDynamicDumpInfo%s", __THEMSG);
855 AOTMetaspace::unrecoverable_loading_error();
856 } else {
858 aot_log_warning(aot)("-XX:ArchiveClassesAtExit" __THEMSG);
859 }
860 #undef __THEMSG
861 disable_dumping_dynamic_archive();
862 return;
863 }
864
865 check_unsupported_dumping_module_options();
866 }
867
868 bool CDSConfig::is_dumping_classic_static_archive() {
869 return _is_dumping_static_archive &&
870 !is_dumping_preimage_static_archive() &&
871 !is_dumping_final_static_archive();
872 }
873
874 bool CDSConfig::is_dumping_preimage_static_archive() {
875 return _is_dumping_preimage_static_archive;
876 }
877
878 bool CDSConfig::is_dumping_preimage_static_archive_with_triggers() {
879 return (!FLAG_IS_DEFAULT(AOTEndTrainingOnMethodEntry)) && is_dumping_preimage_static_archive();
880 }
881
882 bool CDSConfig::is_dumping_final_static_archive() {
883 return _is_dumping_final_static_archive;
884 }
885
886 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) {
887 _is_dumping_dynamic_archive = true;
888 if (output_path == nullptr) {
889 // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo
890 // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual
891 // output path.
892 _output_archive_path = nullptr;
893 } else {
894 _output_archive_path = os::strdup_check_oom(output_path, mtArguments);
895 }
896 }
897
898 bool CDSConfig::allow_only_single_java_thread() {
899 // See comments in JVM_StartThread()
900 return is_dumping_classic_static_archive() || is_dumping_final_static_archive();
901 }
902
903 bool CDSConfig::is_logging_dynamic_proxies() {
904 return ClassListWriter::is_enabled() || is_dumping_preimage_static_archive();
905 }
906
907 // Preserve all states that were examined used during dumptime verification, such
908 // that the verification result (pass or fail) cannot be changed at runtime.
909 //
910 // For example, if the verification of ik requires that class A must be a subtype of B,
911 // then this relationship between A and B cannot be changed at runtime. I.e., the app
912 // cannot load alternative versions of A and B such that A is not a subtype of B.
913 bool CDSConfig::preserve_all_dumptime_verification_states(const InstanceKlass* ik) {
914 // This function will be removed when JDK-8350550 is merged from mainline
915 return ArchivePackages && ArchiveProtectionDomains && is_dumping_aot_linked_classes() && SystemDictionaryShared::is_builtin(ik);
916 }
917
918 bool CDSConfig::is_using_archive() {
919 return UseSharedSpaces;
920 }
921
922 bool CDSConfig::is_using_only_default_archive() {
923 return is_using_archive() &&
924 input_static_archive_path() != nullptr &&
925 default_archive_path() != nullptr &&
926 strcmp(input_static_archive_path(), default_archive_path()) == 0 &&
927 input_dynamic_archive_path() == nullptr;
928 }
929
930 bool CDSConfig::is_logging_lambda_form_invokers() {
931 return ClassListWriter::is_enabled() || is_dumping_dynamic_archive() || is_dumping_preimage_static_archive();
932 }
933
934 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
935 if (is_dumping_final_static_archive()) {
936 // No need to regenerate -- the lambda form invokers should have been regenerated
937 // in the preimage archive (if allowed)
938 return false;
939 } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) {
940 // The base archive has aot-linked classes that may have AOT-resolved CP references
941 // that point to the lambda form invokers in the base archive. Such pointers will
942 // be invalid if lambda form invokers are regenerated in the dynamic archive.
943 return false;
944 } else {
945 return is_dumping_archive();
946 }
947 }
948
949 void CDSConfig::stop_using_optimized_module_handling() {
950 _is_using_optimized_module_handling = false;
951 _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling()
1084 }
1085
1086 bool CDSConfig::is_dumping_aot_linked_classes() {
1087 if (is_dumping_preimage_static_archive()) {
1088 return false;
1089 } else if (is_dumping_dynamic_archive()) {
1090 return is_using_full_module_graph() && AOTClassLinking;
1091 } else if (is_dumping_static_archive()) {
1092 return is_dumping_full_module_graph() && AOTClassLinking;
1093 } else {
1094 return false;
1095 }
1096 }
1097
1098 bool CDSConfig::is_using_aot_linked_classes() {
1099 // Make sure we have the exact same module graph as in the assembly phase, or else
1100 // some aot-linked classes may not be visible so cannot be loaded.
1101 return is_using_full_module_graph() && _has_aot_linked_classes;
1102 }
1103
1104 bool CDSConfig::is_dumping_dynamic_proxies() {
1105 return is_dumping_full_module_graph() && is_dumping_invokedynamic() && ArchiveDynamicProxies;
1106 }
1107
1108 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) {
1109 _has_aot_linked_classes |= has_aot_linked_classes;
1110 }
1111
1112 bool CDSConfig::is_initing_classes_at_dump_time() {
1113 return is_dumping_heap() && is_dumping_aot_linked_classes();
1114 }
1115
1116 bool CDSConfig::is_dumping_invokedynamic() {
1117 // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap
1118 // objects used by the archive indy callsites may be replaced at runtime.
1119 return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap();
1120 }
1121
1122 bool CDSConfig::is_dumping_packages() {
1123 return ArchivePackages && is_dumping_heap();
1124 }
1125
1126 bool CDSConfig::is_loading_packages() {
1127 return UseSharedSpaces && is_using_full_module_graph() && _is_loading_packages;
1128 }
1129
1130 bool CDSConfig::is_dumping_protection_domains() {
1131 if (_is_security_manager_allowed) {
1132 // For sanity, don't archive PDs. TODO: can this be relaxed?
1133 return false;
1134 }
1135 // Archived PDs for the modules will reference their java.lang.Module, which must
1136 // also be archived.
1137 return ArchiveProtectionDomains && is_dumping_full_module_graph();
1138 }
1139
1140 bool CDSConfig::is_loading_protection_domains() {
1141 if (_is_security_manager_allowed) {
1142 // For sanity, don't used any archived PDs. TODO: can this be relaxed?
1143 return false;
1144 }
1145 return UseSharedSpaces && is_using_full_module_graph() && _is_loading_protection_domains;
1146 }
1147
1148 bool CDSConfig::is_dumping_reflection_data() {
1149 // reflection data use LambdaForm classes
1150 return ArchiveReflectionData && is_dumping_invokedynamic();
1151 }
1152
1153 // When we are dumping aot-linked classes and we are able to write archived heap objects, we automatically
1154 // enable the archiving of MethodHandles. This will in turn enable the archiving of MethodTypes and hidden
1155 // classes that are used in the implementation of MethodHandles.
1156 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
1157 // and dynamic proxies.
1158 bool CDSConfig::is_dumping_method_handles() {
1159 return is_initing_classes_at_dump_time();
1160 }
1161
1162 #endif // INCLUDE_CDS_JAVA_HEAP
1163
1164 // AOT code generation and its archiving is disabled by default.
1165 // We enable it only in the final image dump after the metadata and heap are dumped.
1166 // This affects only JITed code because it may have embedded oops and metadata pointers
1167 // which AOT code encodes as offsets in final CDS archive regions.
1168
1169 static bool _is_dumping_aot_code = false;
1170
1171 bool CDSConfig::is_dumping_aot_code() {
1172 return _is_dumping_aot_code;
|