1 /*
  2  * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_CDS_CDSCONFIG_HPP
 26 #define SHARE_CDS_CDSCONFIG_HPP
 27 
 28 #include "memory/allStatic.hpp"
 29 #include "utilities/globalDefinitions.hpp"
 30 #include "utilities/macros.hpp"
 31 
 32 class JavaThread;
 33 class InstanceKlass;
 34 
 35 class CDSConfig : public AllStatic {
 36 #if INCLUDE_CDS
 37   static bool _is_dumping_static_archive;
 38   static bool _is_dumping_preimage_static_archive;
 39   static bool _is_dumping_final_static_archive;
 40   static bool _is_dumping_dynamic_archive;
 41   static bool _is_using_optimized_module_handling;
 42   static bool _is_dumping_full_module_graph;
 43   static bool _is_using_full_module_graph;
 44   static bool _has_aot_linked_classes;
 45   static bool _is_single_command_training;
 46   static bool _has_temp_aot_config_file;
 47   static bool _is_at_aot_safepoint;
 48 
 49   static bool _module_patching_disables_cds;
 50   static bool _java_base_module_patching_disables_cds;
 51 
 52   const static char* _default_archive_path;
 53   const static char* _input_static_archive_path;
 54   const static char* _input_dynamic_archive_path;
 55   const static char* _output_archive_path;
 56 
 57   static bool  _old_cds_flags_used;
 58   static bool  _new_aot_flags_used;
 59   static bool  _disable_heap_dumping;
 60 
 61   static JavaThread* _dumper_thread;
 62 #endif
 63 
 64   static void extract_archive_paths(const char* archive_path,
 65                                     const char** base_archive_path,
 66                                     const char** top_archive_path);
 67   static int num_archive_paths(const char* path_spec);
 68   static void check_flag_single_path(const char* flag_name, const char* value);
 69 
 70   // Checks before Arguments::apply_ergo()
 71   static void check_new_flag(bool new_flag_is_default, const char* new_flag_name);
 72   static void check_aot_flags();
 73   static void check_aotmode_off();
 74   static void check_aotmode_auto_or_on();
 75   static void check_aotmode_record();
 76   static void check_aotmode_create();
 77   static void setup_compiler_args();
 78   static void check_unsupported_dumping_module_options();
 79 
 80   // Called after Arguments::apply_ergo() has started
 81   static void ergo_init_classic_archive_paths();
 82   static void ergo_init_aot_paths();
 83 
 84 public:
 85   // Used by jdk.internal.misc.CDS.getCDSConfigStatus();
 86   static const int IS_DUMPING_AOT_LINKED_CLASSES   = 1 << 0;
 87   static const int IS_DUMPING_ARCHIVE              = 1 << 1;
 88   static const int IS_DUMPING_METHOD_HANDLES       = 1 << 2;
 89   static const int IS_DUMPING_STATIC_ARCHIVE       = 1 << 3;
 90   static const int IS_LOGGING_LAMBDA_FORM_INVOKERS = 1 << 4;
 91   static const int IS_USING_ARCHIVE                = 1 << 5;
 92 
 93   static int get_status() NOT_CDS_RETURN_(0);
 94 
 95   // Initialization and command-line checking
 96   static void ergo_initialize() NOT_CDS_RETURN;
 97   static void set_old_cds_flags_used()                       { CDS_ONLY(_old_cds_flags_used = true); }
 98   static bool old_cds_flags_used()                           { return CDS_ONLY(_old_cds_flags_used) NOT_CDS(false); }
 99   static bool new_aot_flags_used()                           { return CDS_ONLY(_new_aot_flags_used) NOT_CDS(false); }
100   static void check_internal_module_property(const char* key, const char* value) NOT_CDS_RETURN;
101   static void check_incompatible_property(const char* key, const char* value) NOT_CDS_RETURN;
102   static bool has_unsupported_runtime_module_options() NOT_CDS_RETURN_(false);
103   static bool check_vm_args_consistency(bool mode_flag_cmd_line) NOT_CDS_RETURN_(true);
104 
105   static bool module_patching_disables_cds() { return CDS_ONLY(_module_patching_disables_cds) NOT_CDS(false); }
106   static void set_module_patching_disables_cds() { CDS_ONLY(_module_patching_disables_cds = true;) }
107   static bool java_base_module_patching_disables_cds() { return CDS_ONLY(_java_base_module_patching_disables_cds) NOT_CDS(false); }
108   static void set_java_base_module_patching_disables_cds() { CDS_ONLY(_java_base_module_patching_disables_cds = true;) }
109   static const char* type_of_archive_being_loaded();
110   static const char* type_of_archive_being_written();
111   static void prepare_for_dumping();
112 
113   static bool is_at_aot_safepoint()                          { return CDS_ONLY(_is_at_aot_safepoint) NOT_CDS(false); }
114   static void set_is_at_aot_safepoint(bool value)            { CDS_ONLY(_is_at_aot_safepoint = value); }
115 
116   // --- Basic CDS features
117 
118   // archive(s) in general
119   static bool is_dumping_archive()                           { return is_dumping_static_archive() || is_dumping_dynamic_archive(); }
120 
121   // input archive(s)
122   static bool is_using_archive()                             NOT_CDS_RETURN_(false);
123   static bool is_using_only_default_archive()                NOT_CDS_RETURN_(false);
124 
125   // static_archive
126   static bool is_dumping_static_archive()                    { return CDS_ONLY(_is_dumping_static_archive) NOT_CDS(false); }
127   static void enable_dumping_static_archive()                { CDS_ONLY(_is_dumping_static_archive = true); }
128 
129   // A static CDS archive can be dumped in three modes:
130   //
131   // "classic"   - This is the traditional CDS workflow of
132   //               "java -Xshare:dump -XX:SharedClassListFile=file.txt".
133   //
134   // "preimage"  - This happens when we execute the JEP 483 training run, e.g:
135   //               "java -XX:AOTMode=record -XX:AOTConfiguration=app.aotconfig -cp app.jar App"
136   //               The above command writes app.aotconfig as a "CDS preimage". This
137   //               is a binary file that contains all the classes loaded during the
138   //               training run, plus profiling data (e.g., the resolved constant pool entries).
139   //
140   // "final"     - This happens when we execute the JEP 483 assembly phase, e.g:
141   //               "java -XX:AOTMode=create -XX:AOTConfiguration=app.aotconfig -XX:AOTCache=app.aot -cp app.jar"
142   //               The above command loads all classes from app.aotconfig, perform additional linking,
143   //               and writes app.aot as a "CDS final image" file.
144   //
145   // The main structural difference between "preimage" and "final" is that the preimage
146   // - has a different magic number (0xcafea07c)
147   // - does not have any archived Java heap objects
148   // - does not have aot-linked classes
149   static bool is_dumping_classic_static_archive()            NOT_CDS_RETURN_(false);
150   static bool is_dumping_preimage_static_archive()           NOT_CDS_RETURN_(false);
151   static bool is_dumping_final_static_archive()              NOT_CDS_RETURN_(false);
152 
153   // dynamic_archive
154   static bool is_dumping_dynamic_archive()                   { return CDS_ONLY(_is_dumping_dynamic_archive) NOT_CDS(false); }
155   static void enable_dumping_dynamic_archive(const char* output_path) NOT_CDS_RETURN;
156   static void disable_dumping_dynamic_archive()              { CDS_ONLY(_is_dumping_dynamic_archive = false); }
157 
158   // Misc CDS features
159   static bool allow_only_single_java_thread()                NOT_CDS_RETURN_(false);
160 
161   static bool is_single_command_training()                   { return CDS_ONLY(_is_single_command_training) NOT_CDS(false); }
162   static bool has_temp_aot_config_file()                     { return CDS_ONLY(_has_temp_aot_config_file) NOT_CDS(false); }
163 
164   // This is *Legacy* optimization for lambdas before JEP 483. May be removed in the future.
165   static bool is_dumping_lambdas_in_legacy_mode()            NOT_CDS_RETURN_(false);
166 
167   // optimized_module_handling -- can we skip some expensive operations related to modules?
168   static bool is_using_optimized_module_handling()           { return CDS_ONLY(_is_using_optimized_module_handling) NOT_CDS(false); }
169   static void stop_using_optimized_module_handling()         NOT_CDS_RETURN;
170 
171   static bool is_logging_lambda_form_invokers()              NOT_CDS_RETURN_(false);
172   static bool is_dumping_regenerated_lambdaform_invokers()   NOT_CDS_RETURN_(false);
173 
174   static bool is_dumping_aot_linked_classes()                NOT_CDS_JAVA_HEAP_RETURN_(false);
175   static bool is_using_aot_linked_classes()                  NOT_CDS_JAVA_HEAP_RETURN_(false);
176   static void set_has_aot_linked_classes(bool has_aot_linked_classes) NOT_CDS_JAVA_HEAP_RETURN;
177 
178   // Bytecode verification
179   static bool is_preserving_verification_constraints();
180   static bool is_old_class_for_verifier(const InstanceKlass* ik);
181 
182   // archive_path
183 
184   // Points to the classes.jsa in $JAVA_HOME (could be input or output)
185   static const char* default_archive_path()                  NOT_CDS_RETURN_(nullptr);
186   static const char* input_static_archive_path()             { return CDS_ONLY(_input_static_archive_path) NOT_CDS(nullptr); }
187   static const char* input_dynamic_archive_path()            { return CDS_ONLY(_input_dynamic_archive_path) NOT_CDS(nullptr); }
188   static const char* output_archive_path()                   { return CDS_ONLY(_output_archive_path) NOT_CDS(nullptr); }
189 
190   // --- Archived java objects
191 
192   static bool are_vm_options_incompatible_with_dumping_heap() NOT_CDS_JAVA_HEAP_RETURN_(true);
193   static void log_reasons_for_not_dumping_heap();
194 
195   static void disable_heap_dumping()                         { CDS_ONLY(_disable_heap_dumping = true); }
196   static bool is_dumping_heap()                              NOT_CDS_JAVA_HEAP_RETURN_(false);
197   static bool is_loading_heap()                              NOT_CDS_JAVA_HEAP_RETURN_(false);
198 
199   static bool is_dumping_invokedynamic()                     NOT_CDS_JAVA_HEAP_RETURN_(false);
200   static bool is_dumping_method_handles()                    NOT_CDS_JAVA_HEAP_RETURN_(false);
201 
202   // full_module_graph (requires optimized_module_handling)
203   static bool is_dumping_full_module_graph()                 { return CDS_ONLY(_is_dumping_full_module_graph) NOT_CDS(false); }
204   static bool is_using_full_module_graph()                   NOT_CDS_JAVA_HEAP_RETURN_(false);
205   static void stop_dumping_full_module_graph(const char* reason = nullptr) NOT_CDS_JAVA_HEAP_RETURN;
206   static void stop_using_full_module_graph(const char* reason = nullptr) NOT_CDS_JAVA_HEAP_RETURN;
207 
208   // --- AOT code
209 
210   static bool is_dumping_aot_code()                          NOT_CDS_RETURN_(false);
211   static void disable_dumping_aot_code()                     NOT_CDS_RETURN;
212   static void enable_dumping_aot_code()                      NOT_CDS_RETURN;
213   static bool is_dumping_adapters()                          NOT_CDS_RETURN_(false);
214 
215   // Some CDS functions assume that they are called only within a single-threaded context. I.e.,
216   // they are called from:
217   //    - The VM thread (e.g., inside VM_PopulateDumpSharedSpace)
218   //    - The thread that performs prepatory steps before switching to the VM thread
219   // Since these two threads never execute concurrently, we can avoid using locks in these CDS
220   // function. For safety, these functions should assert with CDSConfig::current_thread_is_vm_or_dumper().
221   class DumperThreadMark {
222   public:
223     DumperThreadMark(JavaThread* current);
224     ~DumperThreadMark();
225   };
226 
227   static bool current_thread_is_dumper() NOT_CDS_RETURN_(false);
228   static bool current_thread_is_vm_or_dumper() NOT_CDS_RETURN_(false);
229 };
230 
231 #endif // SHARE_CDS_CDSCONFIG_HPP