1 /*
  2  * Copyright (c) 2023, 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 #include "precompiled.hpp"
 26 #include "cds/archiveHeapLoader.hpp"
 27 #include "cds/cdsConfig.hpp"
 28 #include "cds/cds_globals.hpp"
 29 #include "cds/classListWriter.hpp"
 30 #include "cds/heapShared.hpp"
 31 #include "cds/metaspaceShared.hpp"
 32 #include "classfile/classLoaderDataShared.hpp"
 33 #include "classfile/moduleEntry.hpp"
 34 #include "classfile/systemDictionaryShared.hpp"
 35 #include "include/jvm_io.h"
 36 #include "logging/log.hpp"
 37 #include "prims/jvmtiExport.hpp"
 38 #include "runtime/arguments.hpp"
 39 #include "runtime/globals_extension.hpp"
 40 #include "runtime/java.hpp"
 41 #include "utilities/defaultStream.hpp"
 42 
 43 bool CDSConfig::_is_dumping_static_archive = false;
 44 bool CDSConfig::_is_dumping_dynamic_archive = false;
 45 
 46 // The ability to dump the FMG depends on many factors checked by
 47 // is_dumping_full_module_graph(), but can be unconditionally disabled by
 48 // _dumping_full_module_graph_disabled. (Ditto for loading the FMG).
 49 bool CDSConfig::_dumping_full_module_graph_disabled = false;
 50 bool CDSConfig::_loading_full_module_graph_disabled = false;
 51 bool CDSConfig::_has_preloaded_classes;
 52 bool CDSConfig::_is_loading_invokedynamic = false;
 53 
 54 char* CDSConfig::_default_archive_path = nullptr;
 55 char* CDSConfig::_static_archive_path = nullptr;
 56 char* CDSConfig::_dynamic_archive_path = nullptr;
 57 
 58 void CDSConfig::initialize() {
 59   if (is_dumping_static_archive() && !CDSConfig::is_dumping_final_static_archive()) {
 60     if (RequireSharedSpaces) {
 61       warning("Cannot dump shared archive while using shared archive");
 62     }
 63     UseSharedSpaces = false;
 64   }
 65 
 66   // Initialize shared archive paths which could include both base and dynamic archive paths
 67   // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly.
 68   //
 69   // UseSharedSpaces may be disabled if -XX:SharedArchiveFile is invalid.
 70   if (is_dumping_static_archive() || UseSharedSpaces) {
 71     init_shared_archive_paths();
 72   }
 73 }
 74 
 75 char* CDSConfig::default_archive_path() {
 76   if (_default_archive_path == nullptr) {
 77     char jvm_path[JVM_MAXPATHLEN];
 78     os::jvm_path(jvm_path, sizeof(jvm_path));
 79     char *end = strrchr(jvm_path, *os::file_separator());
 80     if (end != nullptr) *end = '\0';
 81     size_t jvm_path_len = strlen(jvm_path);
 82     size_t file_sep_len = strlen(os::file_separator());
 83     const size_t len = jvm_path_len + file_sep_len + 20;
 84     _default_archive_path = NEW_C_HEAP_ARRAY(char, len, mtArguments);
 85     jio_snprintf(_default_archive_path, len,
 86                 LP64_ONLY(!UseCompressedOops ? "%s%sclasses_nocoops.jsa":) "%s%sclasses.jsa",
 87                 jvm_path, os::file_separator());
 88   }
 89   return _default_archive_path;
 90 }
 91 
 92 int CDSConfig::num_archives(const char* archive_path) {
 93   if (archive_path == nullptr) {
 94     return 0;
 95   }
 96   int npaths = 1;
 97   char* p = (char*)archive_path;
 98   while (*p != '\0') {
 99     if (*p == os::path_separator()[0]) {
100       npaths++;
101     }
102     p++;
103   }
104   return npaths;
105 }
106 
107 void CDSConfig::extract_shared_archive_paths(const char* archive_path,
108                                              char** base_archive_path,
109                                              char** top_archive_path) {
110   char* begin_ptr = (char*)archive_path;
111   char* end_ptr = strchr((char*)archive_path, os::path_separator()[0]);
112   if (end_ptr == nullptr || end_ptr == begin_ptr) {
113     vm_exit_during_initialization("Base archive was not specified", archive_path);
114   }
115   size_t len = end_ptr - begin_ptr;
116   char* cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
117   strncpy(cur_path, begin_ptr, len);
118   cur_path[len] = '\0';
119   *base_archive_path = cur_path;
120 
121   begin_ptr = ++end_ptr;
122   if (*begin_ptr == '\0') {
123     vm_exit_during_initialization("Top archive was not specified", archive_path);
124   }
125   end_ptr = strchr(begin_ptr, '\0');
126   assert(end_ptr != nullptr, "sanity");
127   len = end_ptr - begin_ptr;
128   cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
129   strncpy(cur_path, begin_ptr, len + 1);
130   *top_archive_path = cur_path;
131 }
132 
133 static void set_new_workflow_default_CachedCodeFile() {
134   size_t len = strlen(CacheDataStore) + 6;
135   char* file = AllocateHeap(len, mtArguments);
136   jio_snprintf(file, len, "%s.code", CacheDataStore);
137   FLAG_SET_ERGO(CachedCodeFile, file);
138 }
139 
140 void CDSConfig::init_shared_archive_paths() {
141   if (ArchiveClassesAtExit != nullptr) {
142     assert(!RecordDynamicDumpInfo, "already checked");
143     if (is_dumping_static_archive()) {
144       vm_exit_during_initialization("-XX:ArchiveClassesAtExit cannot be used with -Xshare:dump");
145     }
146     check_unsupported_dumping_properties();
147 
148     if (os::same_files(default_archive_path(), ArchiveClassesAtExit)) {
149       vm_exit_during_initialization(
150         "Cannot specify the default CDS archive for -XX:ArchiveClassesAtExit", default_archive_path());
151     }
152   }
153 
154   if (SharedArchiveFile == nullptr) {
155     _static_archive_path = default_archive_path();
156   } else {
157     int archives = num_archives(SharedArchiveFile);
158     assert(archives > 0, "must be");
159 
160     if (is_dumping_archive() && archives > 1) {
161       vm_exit_during_initialization(
162         "Cannot have more than 1 archive file specified in -XX:SharedArchiveFile during CDS dumping");
163     }
164 
165     if (CDSPreimage != nullptr && archives > 1) {
166       vm_exit_during_initialization("CDSPreimage must point to a single file", CDSPreimage);
167     }
168 
169     if (is_dumping_static_archive()) {
170       assert(archives == 1, "must be");
171       // Static dump is simple: only one archive is allowed in SharedArchiveFile. This file
172       // will be overwritten no matter regardless of its contents
173       _static_archive_path = os::strdup_check_oom(SharedArchiveFile, mtArguments);
174     } else {
175       // SharedArchiveFile may specify one or two files. In case (c), the path for base.jsa
176       // is read from top.jsa
177       //    (a) 1 file:  -XX:SharedArchiveFile=base.jsa
178       //    (b) 2 files: -XX:SharedArchiveFile=base.jsa:top.jsa
179       //    (c) 2 files: -XX:SharedArchiveFile=top.jsa
180       //
181       // However, if either RecordDynamicDumpInfo or ArchiveClassesAtExit is used, we do not
182       // allow cases (b) and (c). Case (b) is already checked above.
183 
184       if (archives > 2) {
185         vm_exit_during_initialization(
186           "Cannot have more than 2 archive files specified in the -XX:SharedArchiveFile option");
187       }
188       if (archives == 1) {
189         char* base_archive_path = nullptr;
190         bool success =
191           FileMapInfo::get_base_archive_name_from_header(SharedArchiveFile, &base_archive_path);
192         if (!success) {
193           if (CDSPreimage != nullptr) {
194             vm_exit_during_initialization("Unable to map shared spaces from CDSPreimage", CDSPreimage);
195           }
196 
197           // If +AutoCreateSharedArchive and the specified shared archive does not exist,
198           // regenerate the dynamic archive base on default archive.
199           if (AutoCreateSharedArchive && !os::file_exists(SharedArchiveFile)) {
200             enable_dumping_dynamic_archive();
201             ArchiveClassesAtExit = const_cast<char *>(SharedArchiveFile);
202             _static_archive_path = default_archive_path();
203             SharedArchiveFile = nullptr;
204           } else {
205             if (AutoCreateSharedArchive) {
206               warning("-XX:+AutoCreateSharedArchive is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info.");
207               AutoCreateSharedArchive = false;
208             }
209             Arguments::no_shared_spaces("invalid archive");
210           }
211         } else if (base_archive_path == nullptr) {
212           // User has specified a single archive, which is a static archive.
213           _static_archive_path = const_cast<char *>(SharedArchiveFile);
214         } else {
215           // User has specified a single archive, which is a dynamic archive.
216           _dynamic_archive_path = const_cast<char *>(SharedArchiveFile);
217           _static_archive_path = base_archive_path; // has been c-heap allocated.
218         }
219       } else {
220         extract_shared_archive_paths((const char*)SharedArchiveFile,
221                                       &_static_archive_path, &_dynamic_archive_path);
222         if (_static_archive_path == nullptr) {
223           assert(_dynamic_archive_path == nullptr, "must be");
224           Arguments::no_shared_spaces("invalid archive");
225         }
226       }
227 
228       if (_dynamic_archive_path != nullptr) {
229         // Check for case (c)
230         if (RecordDynamicDumpInfo) {
231           vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
232                                         SharedArchiveFile);
233         }
234         if (ArchiveClassesAtExit != nullptr) {
235           vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
236                                         SharedArchiveFile);
237         }
238       }
239 
240       if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) {
241           vm_exit_during_initialization(
242             "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit",
243             SharedArchiveFile);
244       }
245     }
246   }
247 }
248 
249 void CDSConfig::check_system_property(const char* key, const char* value) {
250   if (Arguments::is_internal_module_property(key)) {
251     MetaspaceShared::disable_optimized_module_handling();
252     log_info(cds)("optimized module handling: disabled due to incompatible property: %s=%s", key, value);
253   }
254   if (strcmp(key, "jdk.module.showModuleResolution") == 0 ||
255       strcmp(key, "jdk.module.validation") == 0 ||
256       strcmp(key, "java.system.class.loader") == 0) {
257     disable_loading_full_module_graph();
258     disable_dumping_full_module_graph();
259     log_info(cds)("full module graph: disabled due to incompatible property: %s=%s", key, value);
260   }
261 }
262 
263 static const char* unsupported_properties[] = {
264   "jdk.module.limitmods",
265   "jdk.module.upgrade.path",
266   "jdk.module.patch.0"
267 };
268 static const char* unsupported_options[] = {
269   "--limit-modules",
270   "--upgrade-module-path",
271   "--patch-module"
272 };
273 
274 void CDSConfig::check_unsupported_dumping_properties() {
275   assert(is_dumping_archive(), "this function is only used with CDS dump time");
276   assert(ARRAY_SIZE(unsupported_properties) == ARRAY_SIZE(unsupported_options), "must be");
277   // If a vm option is found in the unsupported_options array, vm will exit with an error message.
278   SystemProperty* sp = Arguments::system_properties();
279   while (sp != nullptr) {
280     for (uint i = 0; i < ARRAY_SIZE(unsupported_properties); i++) {
281       if (strcmp(sp->key(), unsupported_properties[i]) == 0) {
282         vm_exit_during_initialization(
283           "Cannot use the following option when dumping the shared archive", unsupported_options[i]);
284       }
285     }
286     sp = sp->next();
287   }
288 
289   // Check for an exploded module build in use with -Xshare:dump.
290   if (!Arguments::has_jimage()) {
291     vm_exit_during_initialization("Dumping the shared archive is not supported with an exploded module build");
292   }
293 }
294 
295 bool CDSConfig::check_unsupported_cds_runtime_properties() {
296   assert(UseSharedSpaces, "this function is only used with -Xshare:{on,auto}");
297   assert(ARRAY_SIZE(unsupported_properties) == ARRAY_SIZE(unsupported_options), "must be");
298   if (ArchiveClassesAtExit != nullptr) {
299     // dynamic dumping, just return false for now.
300     // check_unsupported_dumping_properties() will be called later to check the same set of
301     // properties, and will exit the VM with the correct error message if the unsupported properties
302     // are used.
303     return false;
304   }
305   for (uint i = 0; i < ARRAY_SIZE(unsupported_properties); i++) {
306     if (Arguments::get_property(unsupported_properties[i]) != nullptr) {
307       if (RequireSharedSpaces) {
308         warning("CDS is disabled when the %s option is specified.", unsupported_options[i]);
309       } else {
310         log_info(cds)("CDS is disabled when the %s option is specified.", unsupported_options[i]);
311       }
312       return true;
313     }
314   }
315   return false;
316 }
317 
318 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase,  bool mode_flag_cmd_line) {
319   if (CacheDataStore != nullptr) {
320     if (SharedArchiveFile != nullptr) {
321       vm_exit_during_initialization("CacheDataStore and SharedArchiveFile cannot be both specified");
322     }
323     if (!PreloadSharedClasses) {
324       // TODO: in the forked JVM, we should ensure all classes are loaded from the hotspot.cds.preimage.
325       // PreloadSharedClasses only loads the classes for built-in loaders. We need to load the classes
326       // for custom loaders as well.
327       vm_exit_during_initialization("CacheDataStore requires PreloadSharedClasses");
328     }
329 
330     if (CDSPreimage == nullptr) {
331       if (os::file_exists(CacheDataStore) /* && TODO: CDS file is valid*/) {
332         // The CacheDataStore is already up to date. Use it. Also turn on cached code by default.
333         SharedArchiveFile = CacheDataStore;
334         FLAG_SET_ERGO_IF_DEFAULT(ReplayTraining, true);
335         FLAG_SET_ERGO_IF_DEFAULT(LoadCachedCode, true);
336         if (LoadCachedCode && FLAG_IS_DEFAULT(CachedCodeFile)) {
337           set_new_workflow_default_CachedCodeFile();
338         }
339       } else {
340         // The preimage dumping phase -- run the app and write the preimage file
341         size_t len = strlen(CacheDataStore) + 10;
342         char* preimage = AllocateHeap(len, mtArguments);
343         jio_snprintf(preimage, len, "%s.preimage", CacheDataStore);
344 
345         UseSharedSpaces = false;
346         CDSConfig::enable_dumping_static_archive();
347         SharedArchiveFile = preimage;
348         log_info(cds)("CacheDataStore needs to be updated. Writing %s file", SharedArchiveFile);
349 
350         // At VM exit, the module graph may be contaminated with program states. We should rebuild the
351         // module graph when dumping the CDS final image.
352         log_info(cds)("full module graph: disabled when writing CDS preimage");
353         HeapShared::disable_writing();
354         CDSConfig::disable_dumping_full_module_graph();
355         ArchiveInvokeDynamic = false;
356 
357         FLAG_SET_ERGO_IF_DEFAULT(RecordTraining, true);
358       }
359     } else {
360       // The final image dumping phase -- load the preimage and write the final image file
361       SharedArchiveFile = CDSPreimage;
362       UseSharedSpaces = true;
363       log_info(cds)("Generate CacheDataStore %s from CDSPreimage %s", CacheDataStore, CDSPreimage);
364       // Force -Xbatch for AOT compilation.
365       if (FLAG_SET_CMDLINE(BackgroundCompilation, false) != JVMFlag::SUCCESS) {
366         return false;
367       }
368       Inline = false; // FIXME: this is just for temp debugging.
369       RecordTraining = false; // This will be updated inside MetaspaceShared::preload_and_dump()
370 
371       FLAG_SET_ERGO_IF_DEFAULT(ReplayTraining, true);
372       // Settings for AOT
373       FLAG_SET_ERGO_IF_DEFAULT(StoreCachedCode, true);
374       if (StoreCachedCode && FLAG_IS_DEFAULT(CachedCodeFile)) {
375         set_new_workflow_default_CachedCodeFile();
376         // Cannot dump cached code until metadata and heap are dumped.
377         CDSConfig::disable_dumping_cached_code();
378       }
379     }
380   } else {
381     if (CDSPreimage != nullptr) {
382       vm_exit_during_initialization("CDSPreimage must be specified only when CacheDataStore is specified");
383     }
384   }
385 
386   if (is_dumping_static_archive()) {
387     if (is_dumping_preimage_static_archive() || is_dumping_final_static_archive()) {
388       // Don't tweak execution mode
389     } else if (!mode_flag_cmd_line) {
390       // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
391       //
392       // If your classlist is large and you don't care about deterministic dumping, you can use
393       // -Xshare:dump -Xmixed to improve dumping speed.
394       Arguments::set_mode_flags(Arguments::_int);
395     } else if (Arguments::mode() == Arguments::_comp) {
396       // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
397       // Java code, so there's not much benefit in running -Xcomp.
398       log_info(cds)("reduced -Xcomp to -Xmixed for static dumping");
399       Arguments::set_mode_flags(Arguments::_mixed);
400     }
401 
402     // String deduplication may cause CDS to iterate the strings in different order from one
403     // run to another which resulting in non-determinstic CDS archives.
404     // Disable UseStringDeduplication while dumping CDS archive.
405     UseStringDeduplication = false;
406 
407     Arguments::PropertyList_add(new SystemProperty("java.lang.invoke.MethodHandle.NO_SOFT_CACHE", "true", false));
408   } else {
409     // This flag is useful only when dumping static archive
410     ArchiveInvokeDynamic = false;
411   }
412 
413   // RecordDynamicDumpInfo is not compatible with ArchiveClassesAtExit
414   if (ArchiveClassesAtExit != nullptr && RecordDynamicDumpInfo) {
415     jio_fprintf(defaultStream::output_stream(),
416                 "-XX:+RecordDynamicDumpInfo cannot be used with -XX:ArchiveClassesAtExit.\n");
417     return false;
418   }
419 
420   if (ArchiveClassesAtExit == nullptr && !RecordDynamicDumpInfo) {
421     disable_dumping_dynamic_archive();
422   } else {
423     enable_dumping_dynamic_archive();
424   }
425 
426   if (AutoCreateSharedArchive) {
427     if (SharedArchiveFile == nullptr) {
428       log_warning(cds)("-XX:+AutoCreateSharedArchive requires -XX:SharedArchiveFile");
429       return false;
430     }
431     if (ArchiveClassesAtExit != nullptr) {
432       log_warning(cds)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit");
433       return false;
434     }
435   }
436 
437   if (UseSharedSpaces && patch_mod_javabase) {
438     Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
439   }
440   if (UseSharedSpaces && check_unsupported_cds_runtime_properties()) {
441     UseSharedSpaces = false;
442   }
443 
444   if (is_dumping_archive()) {
445     // Always verify non-system classes during CDS dump
446     if (!BytecodeVerificationRemote) {
447       BytecodeVerificationRemote = true;
448       log_info(cds)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
449     }
450   }
451 
452   if (!CDSConfig::is_dumping_static_archive() || !PreloadSharedClasses) {
453     // FIXME -- CDSConfig::is_dumping_heap() is not yet callable from here, as UseG1GC is not yet set by ergo!
454     //
455     // These optimizations require heap dumping and PreloadSharedClasses, or else
456     // the classes of some archived heap objects may be replaced at runtime.
457     ArchiveInvokeDynamic = false;
458   }
459 
460   if (!ArchiveInvokeDynamic) {
461     ArchiveReflectionData = false; // reflection data use LambdaForm classes
462   }
463 
464   return true;
465 }
466 bool CDSConfig::is_dumping_classic_static_archive() {
467   return _is_dumping_static_archive && CacheDataStore == nullptr && CDSPreimage == nullptr;
468 }
469 
470 bool CDSConfig::is_dumping_preimage_static_archive() {
471   return _is_dumping_static_archive && CacheDataStore != nullptr && CDSPreimage == nullptr;
472 }
473 
474 bool CDSConfig::is_dumping_final_static_archive() {
475   if (CDSPreimage != nullptr) {
476     assert(CacheDataStore != nullptr, "must be"); // should have been properly initialized by arguments.cpp
477   }
478 
479   // Note: _is_dumping_static_archive is false! // FIXME -- refactor this so it makes more sense!
480   return CacheDataStore != nullptr && CDSPreimage != nullptr;
481 }
482 
483 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
484   if (is_dumping_final_static_archive()) {
485     // Not yet supported in new workflow -- the training data may point
486     // to a method in a lambdaform holder class that was not regenerated
487     // due to JDK-8318064.
488     return false;
489   } else {
490     return is_dumping_archive();
491   }
492 }
493 
494 bool CDSConfig::is_tracing_dynamic_proxy() {
495   return ClassListWriter::is_enabled() || is_dumping_preimage_static_archive();
496 }
497 
498 // Preserve all states that were examined used during dumptime verification, such
499 // that the verification result (pass or fail) cannot be changed at runtime.
500 //
501 // For example, if the verification of ik requires that class A must be a subtype of B,
502 // then this relationship between A and B cannot be changed at runtime. I.e., the app
503 // cannot load alternative versions of A and B such that A is not a subtype of B.
504 bool CDSConfig::preserve_all_dumptime_verification_states(const InstanceKlass* ik) {
505   return PreloadSharedClasses && SystemDictionaryShared::is_builtin(ik);
506 }
507 
508 #if INCLUDE_CDS_JAVA_HEAP
509 bool CDSConfig::is_dumping_heap() {
510   return is_dumping_static_archive() && !is_dumping_preimage_static_archive()
511     && HeapShared::can_write();
512 }
513 
514 bool CDSConfig::is_loading_heap() {
515   return ArchiveHeapLoader::is_in_use();
516 }
517 
518 bool CDSConfig::is_dumping_full_module_graph() {
519   if (!_dumping_full_module_graph_disabled &&
520       is_dumping_heap() &&
521       MetaspaceShared::use_optimized_module_handling()) {
522     return true;
523   } else {
524     return false;
525   }
526 }
527 
528 bool CDSConfig::is_loading_full_module_graph() {
529   if (ClassLoaderDataShared::is_full_module_graph_loaded()) {
530     return true;
531   }
532 
533   if (!_loading_full_module_graph_disabled &&
534       UseSharedSpaces &&
535       ArchiveHeapLoader::can_use() &&
536       MetaspaceShared::use_optimized_module_handling()) {
537     // Classes used by the archived full module graph are loaded in JVMTI early phase.
538     assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()),
539            "CDS should be disabled if early class hooks are enabled");
540     return true;
541   } else {
542     return false;
543   }
544 }
545 
546 void CDSConfig::disable_dumping_full_module_graph(const char* reason) {
547   if (!_dumping_full_module_graph_disabled) {
548     _dumping_full_module_graph_disabled = true;
549     if (reason != nullptr) {
550       log_info(cds)("full module graph cannot be dumped: %s", reason);
551     }
552   }
553 }
554 
555 void CDSConfig::disable_loading_full_module_graph(const char* reason) {
556   assert(!ClassLoaderDataShared::is_full_module_graph_loaded(), "you call this function too late!");
557   if (!_loading_full_module_graph_disabled) {
558     _loading_full_module_graph_disabled = true;
559     if (reason != nullptr) {
560       log_info(cds)("full module graph cannot be loaded: %s", reason);
561     }
562   }
563 }
564 
565 bool CDSConfig::is_loading_invokedynamic() {
566   return UseSharedSpaces && is_loading_heap() && _is_loading_invokedynamic;
567 }
568 
569 bool CDSConfig::is_dumping_dynamic_proxy() {
570   return is_dumping_full_module_graph() && is_dumping_invokedynamic();
571 }
572 
573 bool CDSConfig::is_initing_classes_at_dump_time() {
574   return is_dumping_heap() && PreloadSharedClasses;
575 }
576 
577 bool CDSConfig::is_dumping_invokedynamic() {
578   return ArchiveInvokeDynamic && is_dumping_heap();
579 }
580 #endif // INCLUDE_CDS_JAVA_HEAP
581 
582 // This is allowed by default. We disable it only in the final image dump before the
583 // metadata and heap are dumped.
584 static bool _is_dumping_cached_code = true;
585 
586 bool CDSConfig::is_dumping_cached_code() {
587   return _is_dumping_cached_code;
588 }
589 
590 void CDSConfig::disable_dumping_cached_code() {
591   _is_dumping_cached_code = false;
592 }
593 
594 void CDSConfig::enable_dumping_cached_code() {
595   _is_dumping_cached_code = true;
596 }