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