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 #include "cds/archiveHeapLoader.hpp" 26 #include "cds/cdsConfig.hpp" 27 #include "cds/cds_globals.hpp" 28 #include "cds/classListWriter.hpp" 29 #include "cds/filemap.hpp" 30 #include "cds/heapShared.hpp" 31 #include "cds/metaspaceShared.hpp" 32 #include "classfile/classLoaderDataShared.hpp" 33 #include "classfile/moduleEntry.hpp" 34 #include "classfile/systemDictionaryShared.hpp" 35 #include "code/SCCache.hpp" 36 #include "include/jvm_io.h" 37 #include "logging/log.hpp" 38 #include "prims/jvmtiExport.hpp" 39 #include "memory/universe.hpp" 40 #include "runtime/arguments.hpp" 41 #include "runtime/globals_extension.hpp" 42 #include "runtime/java.hpp" 43 #include "runtime/vmThread.hpp" 44 #include "utilities/defaultStream.hpp" 45 #include "utilities/formatBuffer.hpp" 46 47 bool CDSConfig::_is_dumping_static_archive = false; 48 bool CDSConfig::_is_dumping_preimage_static_archive = false; 49 bool CDSConfig::_is_dumping_final_static_archive = false; 50 bool CDSConfig::_is_dumping_dynamic_archive = false; 51 bool CDSConfig::_is_using_optimized_module_handling = true; 52 bool CDSConfig::_is_dumping_full_module_graph = true; 53 bool CDSConfig::_is_using_full_module_graph = true; 54 bool CDSConfig::_has_aot_linked_classes = false; 55 bool CDSConfig::_has_archived_invokedynamic = false; 56 bool CDSConfig::_is_loading_packages = false; 57 bool CDSConfig::_is_loading_protection_domains = false; 58 bool CDSConfig::_is_security_manager_allowed = false; 59 bool CDSConfig::_old_cds_flags_used = false; 60 bool CDSConfig::_new_aot_flags_used = false; 61 bool CDSConfig::_disable_heap_dumping = false; 62 63 char* CDSConfig::_default_archive_path = nullptr; 64 char* CDSConfig::_static_archive_path = nullptr; 65 char* CDSConfig::_dynamic_archive_path = nullptr; 66 67 JavaThread* CDSConfig::_dumper_thread = nullptr; 68 69 int CDSConfig::get_status() { 70 assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized"); 71 return (is_dumping_archive() ? IS_DUMPING_ARCHIVE : 0) | 72 (is_dumping_static_archive() ? IS_DUMPING_STATIC_ARCHIVE : 0) | 73 (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) | 74 (is_using_archive() ? IS_USING_ARCHIVE : 0) | 75 (is_dumping_heap() ? IS_DUMPING_HEAP : 0) | 76 (is_logging_dynamic_proxies() ? IS_LOGGING_DYNAMIC_PROXIES : 0) | 77 (is_dumping_packages() ? IS_DUMPING_PACKAGES : 0) | 78 (is_dumping_protection_domains() ? IS_DUMPING_PROTECTION_DOMAINS : 0); 79 } 80 81 void CDSConfig::initialize() { 82 if (is_dumping_static_archive() && !is_dumping_final_static_archive()) { 83 // If dumping the classic archive, or making an AOT training run (dumping a preimage archive), 84 // for sanity, parse all classes from classfiles. 85 // TODO: in the future, if we want to support re-training on top of an existing AOT cache, this 86 // needs to be changed. 87 if (RequireSharedSpaces) { 88 if (is_experimental_leyden_workflow()) { 89 log_info(cds)("-Xshare:on flag is ignored when creating a CacheDataStore"); 90 } else { 91 // -Xshare and -XX:AOTMode flags are mutually exclusive: 92 // Class workflow: -Xshare:on and -Xshare:dump cannot take effect at the same time. 93 // JEP 483 workflow: -XX:AOTMode:record and -XX:AOTMode=on cannot take effect at the same time. 94 ShouldNotReachHere(); 95 } 96 } 97 UseSharedSpaces = false; 98 } 99 100 // Initialize shared archive paths which could include both base and dynamic archive paths 101 // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly. 102 // 103 // UseSharedSpaces may be disabled if -XX:SharedArchiveFile is invalid. 104 if (is_dumping_static_archive() || is_using_archive()) { 105 init_shared_archive_paths(); 106 } 107 108 if (!is_dumping_heap()) { 109 _is_dumping_full_module_graph = false; 110 } 111 } 112 113 char* CDSConfig::default_archive_path() { 114 if (_default_archive_path == nullptr) { 115 char jvm_path[JVM_MAXPATHLEN]; 116 os::jvm_path(jvm_path, sizeof(jvm_path)); 117 char *end = strrchr(jvm_path, *os::file_separator()); 118 if (end != nullptr) *end = '\0'; 119 stringStream tmp; 120 tmp.print("%s%sclasses", jvm_path, os::file_separator()); 121 #ifdef _LP64 122 if (!UseCompressedOops) { 123 tmp.print_raw("_nocoops"); 124 } 125 if (UseCompactObjectHeaders) { 126 // Note that generation of xxx_coh.jsa variants require 127 // --enable-cds-archive-coh at build time 128 tmp.print_raw("_coh"); 129 } 130 #endif 131 tmp.print_raw(".jsa"); 132 _default_archive_path = os::strdup(tmp.base()); 133 } 134 return _default_archive_path; 135 } 136 137 int CDSConfig::num_archives(const char* archive_path) { 138 if (archive_path == nullptr) { 139 return 0; 140 } 141 int npaths = 1; 142 char* p = (char*)archive_path; 143 while (*p != '\0') { 144 if (*p == os::path_separator()[0]) { 145 npaths++; 146 } 147 p++; 148 } 149 return npaths; 150 } 151 152 void CDSConfig::extract_shared_archive_paths(const char* archive_path, 153 char** base_archive_path, 154 char** top_archive_path) { 155 char* begin_ptr = (char*)archive_path; 156 char* end_ptr = strchr((char*)archive_path, os::path_separator()[0]); 157 if (end_ptr == nullptr || end_ptr == begin_ptr) { 158 vm_exit_during_initialization("Base archive was not specified", archive_path); 159 } 160 size_t len = end_ptr - begin_ptr; 161 char* cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal); 162 strncpy(cur_path, begin_ptr, len); 163 cur_path[len] = '\0'; 164 *base_archive_path = cur_path; 165 166 begin_ptr = ++end_ptr; 167 if (*begin_ptr == '\0') { 168 vm_exit_during_initialization("Top archive was not specified", archive_path); 169 } 170 end_ptr = strchr(begin_ptr, '\0'); 171 assert(end_ptr != nullptr, "sanity"); 172 len = end_ptr - begin_ptr; 173 cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal); 174 strncpy(cur_path, begin_ptr, len + 1); 175 *top_archive_path = cur_path; 176 } 177 178 void CDSConfig::init_shared_archive_paths() { 179 if (ArchiveClassesAtExit != nullptr) { 180 assert(!RecordDynamicDumpInfo, "already checked"); 181 if (is_dumping_static_archive()) { 182 vm_exit_during_initialization("-XX:ArchiveClassesAtExit cannot be used with -Xshare:dump"); 183 } 184 check_unsupported_dumping_module_options(); 185 186 if (os::same_files(default_archive_path(), ArchiveClassesAtExit)) { 187 vm_exit_during_initialization( 188 "Cannot specify the default CDS archive for -XX:ArchiveClassesAtExit", default_archive_path()); 189 } 190 } 191 192 if (SharedArchiveFile == nullptr) { 193 _static_archive_path = default_archive_path(); 194 } else { 195 int archives = num_archives(SharedArchiveFile); 196 assert(archives > 0, "must be"); 197 198 if (is_dumping_archive() && archives > 1) { 199 vm_exit_during_initialization( 200 "Cannot have more than 1 archive file specified in -XX:SharedArchiveFile during CDS dumping"); 201 } 202 203 if (CDSPreimage != nullptr && archives > 1) { 204 vm_exit_during_initialization("CDSPreimage must point to a single file", CDSPreimage); 205 } 206 207 if (is_dumping_static_archive()) { 208 assert(archives == 1, "must be"); 209 // Static dump is simple: only one archive is allowed in SharedArchiveFile. This file 210 // will be overwritten no matter regardless of its contents 211 _static_archive_path = os::strdup_check_oom(SharedArchiveFile, mtArguments); 212 } else { 213 // SharedArchiveFile may specify one or two files. In case (c), the path for base.jsa 214 // is read from top.jsa 215 // (a) 1 file: -XX:SharedArchiveFile=base.jsa 216 // (b) 2 files: -XX:SharedArchiveFile=base.jsa:top.jsa 217 // (c) 2 files: -XX:SharedArchiveFile=top.jsa 218 // 219 // However, if either RecordDynamicDumpInfo or ArchiveClassesAtExit is used, we do not 220 // allow cases (b) and (c). Case (b) is already checked above. 221 222 if (archives > 2) { 223 vm_exit_during_initialization( 224 "Cannot have more than 2 archive files specified in the -XX:SharedArchiveFile option"); 225 } 226 if (archives == 1) { 227 char* base_archive_path = nullptr; 228 bool success = 229 FileMapInfo::get_base_archive_name_from_header(SharedArchiveFile, &base_archive_path); 230 if (!success) { 231 if (CDSPreimage != nullptr) { 232 vm_exit_during_initialization("Unable to map shared spaces from CDSPreimage", CDSPreimage); 233 } 234 235 // If +AutoCreateSharedArchive and the specified shared archive does not exist, 236 // regenerate the dynamic archive base on default archive. 237 if (AutoCreateSharedArchive && !os::file_exists(SharedArchiveFile)) { 238 enable_dumping_dynamic_archive(); 239 ArchiveClassesAtExit = const_cast<char *>(SharedArchiveFile); 240 _static_archive_path = default_archive_path(); 241 SharedArchiveFile = nullptr; 242 } else { 243 if (AutoCreateSharedArchive) { 244 warning("-XX:+AutoCreateSharedArchive is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."); 245 AutoCreateSharedArchive = false; 246 } 247 log_error(cds)("Not a valid %s (%s)", new_aot_flags_used() ? "AOT cache" : "archive", SharedArchiveFile); 248 Arguments::no_shared_spaces("invalid archive"); 249 } 250 } else if (base_archive_path == nullptr) { 251 // User has specified a single archive, which is a static archive. 252 _static_archive_path = const_cast<char *>(SharedArchiveFile); 253 } else { 254 // User has specified a single archive, which is a dynamic archive. 255 _dynamic_archive_path = const_cast<char *>(SharedArchiveFile); 256 _static_archive_path = base_archive_path; // has been c-heap allocated. 257 } 258 } else { 259 extract_shared_archive_paths((const char*)SharedArchiveFile, 260 &_static_archive_path, &_dynamic_archive_path); 261 if (_static_archive_path == nullptr) { 262 assert(_dynamic_archive_path == nullptr, "must be"); 263 Arguments::no_shared_spaces("invalid archive"); 264 } 265 } 266 267 if (_dynamic_archive_path != nullptr) { 268 // Check for case (c) 269 if (RecordDynamicDumpInfo) { 270 vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile", 271 SharedArchiveFile); 272 } 273 if (ArchiveClassesAtExit != nullptr) { 274 vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile", 275 SharedArchiveFile); 276 } 277 } 278 279 if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) { 280 vm_exit_during_initialization( 281 "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit", 282 SharedArchiveFile); 283 } 284 } 285 } 286 } 287 288 static char* bad_module_prop_key = nullptr; 289 static char* bad_module_prop_value = nullptr; 290 291 void CDSConfig::check_internal_module_property(const char* key, const char* value) { 292 if (Arguments::is_incompatible_cds_internal_module_property(key)) { 293 stop_using_optimized_module_handling(); 294 if (bad_module_prop_key == nullptr) { 295 // We don't want to print an unconditional warning here, as we are still processing the command line. 296 // A later argument may specify something like -Xshare:off, which makes such a warning irrelevant. 297 // 298 // Instead, we save the info so we can warn when necessary: we are doing it only during CacheDataStore 299 // creation for now, but could add it to other places. 300 bad_module_prop_key = os::strdup(key); 301 bad_module_prop_value = os::strdup(value); 302 } 303 log_info(cds)("optimized module handling/full module graph: disabled due to incompatible property: %s=%s", key, value); 304 } 305 } 306 307 void CDSConfig::check_incompatible_property(const char* key, const char* value) { 308 static const char* incompatible_properties[] = { 309 "java.system.class.loader", 310 "jdk.module.showModuleResolution", 311 "jdk.module.validation" 312 }; 313 314 for (const char* property : incompatible_properties) { 315 if (strcmp(key, property) == 0) { 316 stop_dumping_full_module_graph(); 317 stop_using_full_module_graph(); 318 log_info(cds)("full module graph: disabled due to incompatible property: %s=%s", key, value); 319 break; 320 } 321 } 322 323 // Match the logic in java/lang/System.java, but we need to know this before the System class is initialized. 324 if (strcmp(key, "java.security.manager") == 0) { 325 if (strcmp(value, "disallowed") != 0) { 326 _is_security_manager_allowed = true; 327 } 328 } 329 } 330 331 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS. 332 static const char* find_any_unsupported_module_option() { 333 // Note that arguments.cpp has translated the command-line options into properties. If we find an 334 // unsupported property, translate it back to its command-line option for better error reporting. 335 336 // The following properties are checked by Arguments::is_internal_module_property() and cannot be 337 // directly specified in the command-line. 338 static const char* unsupported_module_properties[] = { 339 "jdk.module.limitmods", 340 "jdk.module.upgrade.path", 341 "jdk.module.patch.0" 342 }; 343 static const char* unsupported_module_options[] = { 344 "--limit-modules", 345 "--upgrade-module-path", 346 "--patch-module" 347 }; 348 349 assert(ARRAY_SIZE(unsupported_module_properties) == ARRAY_SIZE(unsupported_module_options), "must be"); 350 SystemProperty* sp = Arguments::system_properties(); 351 while (sp != nullptr) { 352 for (uint i = 0; i < ARRAY_SIZE(unsupported_module_properties); i++) { 353 if (strcmp(sp->key(), unsupported_module_properties[i]) == 0) { 354 return unsupported_module_options[i]; 355 } 356 } 357 sp = sp->next(); 358 } 359 360 return nullptr; // not found 361 } 362 363 void CDSConfig::check_unsupported_dumping_module_options() { 364 assert(is_dumping_archive(), "this function is only used with CDS dump time"); 365 const char* option = find_any_unsupported_module_option(); 366 if (option != nullptr) { 367 vm_exit_during_initialization("Cannot use the following option when dumping the shared archive", option); 368 } 369 // Check for an exploded module build in use with -Xshare:dump. 370 if (!Arguments::has_jimage()) { 371 vm_exit_during_initialization("Dumping the shared archive is not supported with an exploded module build"); 372 } 373 } 374 375 bool CDSConfig::has_unsupported_runtime_module_options() { 376 assert(is_using_archive(), "this function is only used with -Xshare:{on,auto}"); 377 if (ArchiveClassesAtExit != nullptr) { 378 // dynamic dumping, just return false for now. 379 // check_unsupported_dumping_properties() will be called later to check the same set of 380 // properties, and will exit the VM with the correct error message if the unsupported properties 381 // are used. 382 return false; 383 } 384 const char* option = find_any_unsupported_module_option(); 385 if (option != nullptr) { 386 if (RequireSharedSpaces) { 387 warning("CDS is disabled when the %s option is specified.", option); 388 } else { 389 if (new_aot_flags_used()) { 390 log_warning(cds)("AOT cache is disabled when the %s option is specified.", option); 391 } else { 392 log_info(cds)("CDS is disabled when the %s option is specified.", option); 393 } 394 } 395 return true; 396 } 397 return false; 398 } 399 400 #define CHECK_ALIAS(f) check_flag_alias(FLAG_IS_DEFAULT(f), #f) 401 402 void CDSConfig::check_flag_alias(bool alias_is_default, const char* alias_name) { 403 if (old_cds_flags_used() && !alias_is_default) { 404 vm_exit_during_initialization(err_msg("Option %s cannot be used at the same time with " 405 "-Xshare:on, -Xshare:auto, -Xshare:off, -Xshare:dump, " 406 "DumpLoadedClassList, SharedClassListFile, or SharedArchiveFile", 407 alias_name)); 408 } 409 } 410 411 void CDSConfig::check_aot_flags() { 412 if (!FLAG_IS_DEFAULT(DumpLoadedClassList) || 413 !FLAG_IS_DEFAULT(SharedClassListFile) || 414 !FLAG_IS_DEFAULT(SharedArchiveFile)) { 415 _old_cds_flags_used = true; 416 } 417 418 CHECK_ALIAS(AOTCache); 419 CHECK_ALIAS(AOTConfiguration); 420 CHECK_ALIAS(AOTMode); 421 422 if (FLAG_IS_DEFAULT(AOTCache) && FLAG_IS_DEFAULT(AOTConfiguration) && FLAG_IS_DEFAULT(AOTMode)) { 423 // AOTCache/AOTConfiguration/AOTMode not used. 424 return; 425 } else { 426 _new_aot_flags_used = true; 427 } 428 429 if (FLAG_IS_DEFAULT(AOTMode) || strcmp(AOTMode, "auto") == 0 || strcmp(AOTMode, "on") == 0) { 430 check_aotmode_auto_or_on(); 431 } else if (strcmp(AOTMode, "off") == 0) { 432 check_aotmode_off(); 433 } else { 434 // AOTMode is record or create 435 if (FLAG_IS_DEFAULT(AOTConfiguration)) { 436 vm_exit_during_initialization(err_msg("-XX:AOTMode=%s cannot be used without setting AOTConfiguration", AOTMode)); 437 } 438 439 if (strcmp(AOTMode, "record") == 0) { 440 check_aotmode_record(); 441 } else { 442 assert(strcmp(AOTMode, "create") == 0, "checked by AOTModeConstraintFunc"); 443 check_aotmode_create(); 444 } 445 } 446 } 447 448 void CDSConfig::check_aotmode_off() { 449 UseSharedSpaces = false; 450 RequireSharedSpaces = false; 451 } 452 453 void CDSConfig::check_aotmode_auto_or_on() { 454 if (!FLAG_IS_DEFAULT(AOTConfiguration)) { 455 vm_exit_during_initialization("AOTConfiguration can only be used with -XX:AOTMode=record or -XX:AOTMode=create"); 456 } 457 458 if (!FLAG_IS_DEFAULT(AOTCache)) { 459 assert(FLAG_IS_DEFAULT(SharedArchiveFile), "already checked"); 460 FLAG_SET_ERGO(SharedArchiveFile, AOTCache); 461 } 462 463 UseSharedSpaces = true; 464 if (FLAG_IS_DEFAULT(AOTMode) || (strcmp(AOTMode, "auto") == 0)) { 465 RequireSharedSpaces = false; 466 } else { 467 assert(strcmp(AOTMode, "on") == 0, "already checked"); 468 RequireSharedSpaces = true; 469 } 470 } 471 472 void CDSConfig::check_aotmode_record() { 473 if (!FLAG_IS_DEFAULT(AOTCache)) { 474 vm_exit_during_initialization("AOTCache must not be specified when using -XX:AOTMode=record"); 475 } 476 477 assert(FLAG_IS_DEFAULT(DumpLoadedClassList), "already checked"); 478 assert(FLAG_IS_DEFAULT(SharedArchiveFile), "already checked"); 479 FLAG_SET_ERGO(SharedArchiveFile, AOTConfiguration); 480 FLAG_SET_ERGO(DumpLoadedClassList, nullptr); 481 UseSharedSpaces = false; 482 RequireSharedSpaces = false; 483 _is_dumping_static_archive = true; 484 _is_dumping_preimage_static_archive = true; 485 486 // At VM exit, the module graph may be contaminated with program states. 487 // We will rebuild the module graph when dumping the CDS final image. 488 disable_heap_dumping(); 489 } 490 491 void CDSConfig::check_aotmode_create() { 492 if (FLAG_IS_DEFAULT(AOTCache)) { 493 vm_exit_during_initialization("AOTCache must be specified when using -XX:AOTMode=create"); 494 } 495 496 assert(FLAG_IS_DEFAULT(SharedArchiveFile), "already checked"); 497 498 _is_dumping_final_static_archive = true; 499 FLAG_SET_ERGO(SharedArchiveFile, AOTConfiguration); 500 UseSharedSpaces = true; 501 RequireSharedSpaces = true; 502 503 if (!FileMapInfo::is_preimage_static_archive(AOTConfiguration)) { 504 vm_exit_during_initialization("Must be a valid AOT configuration generated by the current JVM", AOTConfiguration); 505 } 506 507 CDSConfig::enable_dumping_static_archive(); 508 } 509 510 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line, bool xshare_auto_cmd_line) { 511 check_aot_flags(); 512 513 if (!FLAG_IS_DEFAULT(AOTMode)) { 514 // Using any form of the new AOTMode switch enables enhanced optimizations. 515 FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true); 516 } 517 518 if (CacheDataStore != nullptr) { 519 if (!setup_experimental_leyden_workflow(xshare_auto_cmd_line)) { 520 return false; 521 } 522 } else { 523 if (CDSPreimage != nullptr) { 524 vm_exit_during_initialization("CDSPreimage must be specified only when CacheDataStore is specified"); 525 } 526 527 setup_compiler_args(); 528 } 529 530 if (AOTClassLinking) { 531 // If AOTClassLinking is specified, enable all these optimizations by default. 532 FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true); 533 FLAG_SET_ERGO_IF_DEFAULT(ArchiveDynamicProxies, true); 534 FLAG_SET_ERGO_IF_DEFAULT(ArchiveLoaderLookupCache, true); 535 FLAG_SET_ERGO_IF_DEFAULT(ArchivePackages, true); 536 FLAG_SET_ERGO_IF_DEFAULT(ArchiveProtectionDomains, true); 537 FLAG_SET_ERGO_IF_DEFAULT(ArchiveReflectionData, true); 538 } else { 539 // All of these *might* depend on AOTClassLinking. Better be safe than sorry. 540 FLAG_SET_ERGO(AOTInvokeDynamicLinking, false); 541 FLAG_SET_ERGO(ArchiveDynamicProxies, false); 542 FLAG_SET_ERGO(ArchiveLoaderLookupCache, false); 543 FLAG_SET_ERGO(ArchivePackages, false); 544 FLAG_SET_ERGO(ArchiveProtectionDomains, false); 545 FLAG_SET_ERGO(ArchiveReflectionData, false); 546 547 if (CDSConfig::is_dumping_archive()) { 548 FLAG_SET_ERGO(RecordTraining, false); 549 FLAG_SET_ERGO(ReplayTraining, false); 550 FLAG_SET_ERGO(StoreCachedCode, false); 551 FLAG_SET_ERGO(LoadCachedCode, false); 552 } 553 } 554 555 if (StoreCachedCode) { 556 log_info(cds)("ArchiveAdapters is enabled"); 557 FLAG_SET_ERGO_IF_DEFAULT(ArchiveAdapters, true); 558 } 559 560 #ifdef _WINDOWS 561 // This optimization is not working on Windows for some reason. See JDK-8338604. 562 FLAG_SET_ERGO(ArchiveReflectionData, false); 563 #endif 564 565 if (is_dumping_static_archive()) { 566 if (is_dumping_preimage_static_archive() || is_dumping_final_static_archive()) { 567 // Don't tweak execution mode 568 } else if (!mode_flag_cmd_line) { 569 // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive. 570 // 571 // If your classlist is large and you don't care about deterministic dumping, you can use 572 // -Xshare:dump -Xmixed to improve dumping speed. 573 Arguments::set_mode_flags(Arguments::_int); 574 } else if (Arguments::mode() == Arguments::_comp) { 575 // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of 576 // Java code, so there's not much benefit in running -Xcomp. 577 log_info(cds)("reduced -Xcomp to -Xmixed for static dumping"); 578 Arguments::set_mode_flags(Arguments::_mixed); 579 } 580 581 // String deduplication may cause CDS to iterate the strings in different order from one 582 // run to another which resulting in non-determinstic CDS archives. 583 // Disable UseStringDeduplication while dumping CDS archive. 584 UseStringDeduplication = false; 585 } 586 587 // RecordDynamicDumpInfo is not compatible with ArchiveClassesAtExit 588 if (ArchiveClassesAtExit != nullptr && RecordDynamicDumpInfo) { 589 jio_fprintf(defaultStream::output_stream(), 590 "-XX:+RecordDynamicDumpInfo cannot be used with -XX:ArchiveClassesAtExit.\n"); 591 return false; 592 } 593 594 if (ArchiveClassesAtExit == nullptr && !RecordDynamicDumpInfo) { 595 disable_dumping_dynamic_archive(); 596 } else { 597 enable_dumping_dynamic_archive(); 598 } 599 600 if (AutoCreateSharedArchive) { 601 if (SharedArchiveFile == nullptr) { 602 log_warning(cds)("-XX:+AutoCreateSharedArchive requires -XX:SharedArchiveFile"); 603 return false; 604 } 605 if (ArchiveClassesAtExit != nullptr) { 606 log_warning(cds)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit"); 607 return false; 608 } 609 } 610 611 if (is_using_archive() && patch_mod_javabase) { 612 Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched."); 613 } 614 if (is_using_archive() && has_unsupported_runtime_module_options()) { 615 UseSharedSpaces = false; 616 } 617 618 if (is_dumping_archive()) { 619 // Always verify non-system classes during CDS dump 620 if (!BytecodeVerificationRemote) { 621 BytecodeVerificationRemote = true; 622 log_info(cds)("All non-system classes will be verified (-Xverify:remote) during CDS dump time."); 623 } 624 } 625 626 if (AOTClassLinking) { 627 if (is_dumping_final_static_archive() && !is_dumping_full_module_graph()) { 628 if (bad_module_prop_key != nullptr) { 629 log_warning(cds)("optimized module handling/full module graph: disabled due to incompatible property: %s=%s", 630 bad_module_prop_key, bad_module_prop_value); 631 } 632 if (is_experimental_leyden_workflow()) { 633 vm_exit_during_initialization("CacheDataStore cannot be created because AOTClassLinking is enabled but full module graph is disabled"); 634 } else { 635 vm_exit_during_initialization("AOT cache cannot be created because AOTClassLinking is enabled but full module graph is disabled"); 636 } 637 } 638 } 639 640 return true; 641 } 642 643 void CDSConfig::setup_compiler_args() { 644 // AOT profiles and AOT-compiled methods are supported only in the JEP 483 workflow. 645 bool can_dump_profile_and_compiled_code = AOTClassLinking && new_aot_flags_used(); 646 647 if (is_dumping_preimage_static_archive() && can_dump_profile_and_compiled_code) { 648 // JEP 483 workflow -- training 649 FLAG_SET_ERGO_IF_DEFAULT(RecordTraining, true); 650 FLAG_SET_ERGO(ReplayTraining, false); 651 FLAG_SET_ERGO(StoreCachedCode, false); 652 FLAG_SET_ERGO(LoadCachedCode, false); 653 } else if (is_dumping_final_static_archive() && can_dump_profile_and_compiled_code) { 654 // JEP 483 workflow -- assembly 655 FLAG_SET_ERGO(RecordTraining, false); // This will be updated inside MetaspaceShared::preload_and_dump() 656 FLAG_SET_ERGO_IF_DEFAULT(ReplayTraining, true); 657 FLAG_SET_ERGO_IF_DEFAULT(StoreCachedCode, true); 658 FLAG_SET_ERGO(LoadCachedCode, false); 659 disable_dumping_cached_code(); // Cannot dump cached code until metadata and heap are dumped. 660 } else if (is_using_archive() && new_aot_flags_used()) { 661 // JEP 483 workflow -- production 662 FLAG_SET_ERGO(RecordTraining, false); 663 FLAG_SET_ERGO_IF_DEFAULT(ReplayTraining, true); 664 FLAG_SET_ERGO(StoreCachedCode, false); 665 FLAG_SET_ERGO_IF_DEFAULT(LoadCachedCode, true); 666 667 if (UseSharedSpaces && FLAG_IS_DEFAULT(AOTMode)) { 668 log_info(cds)("Enabled -XX:AOTMode=on by default for troubleshooting Leyden prototype"); 669 RequireSharedSpaces = true; 670 } 671 } else { 672 FLAG_SET_ERGO(ReplayTraining, false); 673 FLAG_SET_ERGO(RecordTraining, false); 674 FLAG_SET_ERGO(StoreCachedCode, false); 675 FLAG_SET_ERGO(LoadCachedCode, false); 676 } 677 } 678 679 // Ergo set-up of various flags used by the experimental workflow that uses -XX:CacheDataStore. This workflow 680 // is deprecated and will be removed from Leyden. 681 bool CDSConfig::setup_experimental_leyden_workflow(bool xshare_auto_cmd_line) { 682 // Leyden temp work-around: 683 // 684 // By default, when using CacheDataStore, use the HeapBasedNarrowOop mode so that 685 // AOT code can be always work regardless of runtime heap range. 686 // 687 // If you are *absolutely sure* that the CompressedOops::mode() will be the same 688 // between training and production runs (e.g., if you specify -Xmx128m 689 // for both training and production runs, and you know the OS will always reserve 690 // the heap under 4GB), you can explicitly disable this with: 691 // java -XX:-UseCompatibleCompressedOops -XX:CacheDataStore=... 692 // However, this is risky and there's a chance that the production run will be slower 693 // because it is unable to load the AOT code cache. 694 #ifdef _LP64 695 // FLAG_SET_ERGO_IF_DEFAULT(UseCompatibleCompressedOops, true); // FIXME @iklam - merge with mainline - UseCompatibleCompressedOops 696 #endif 697 698 if (FLAG_IS_DEFAULT(AOTClassLinking)) { 699 FLAG_SET_ERGO(AOTClassLinking, true); 700 } 701 702 if (SharedArchiveFile != nullptr) { 703 vm_exit_during_initialization("CacheDataStore and SharedArchiveFile cannot be both specified"); 704 } 705 if (!AOTClassLinking) { 706 vm_exit_during_initialization("CacheDataStore requires AOTClassLinking"); 707 } 708 709 if (CDSPreimage == nullptr) { 710 if (os::file_exists(CacheDataStore) /* && TODO: Need to check if CDS file is valid*/) { 711 // The CacheDataStore is already up to date. Use it. Also turn on cached code by default. 712 SharedArchiveFile = CacheDataStore; 713 FLAG_SET_ERGO_IF_DEFAULT(ReplayTraining, true); 714 FLAG_SET_ERGO_IF_DEFAULT(LoadCachedCode, true); 715 716 // Leyden temp: make sure the user knows if CDS archive somehow fails to load. 717 if (UseSharedSpaces && !xshare_auto_cmd_line) { 718 log_info(cds)("Enabled -Xshare:on by default for troubleshooting Leyden prototype"); 719 RequireSharedSpaces = true; 720 } 721 } else { 722 // The preimage dumping phase -- run the app and write the preimage file 723 size_t len = strlen(CacheDataStore) + 10; 724 char* preimage = AllocateHeap(len, mtArguments); 725 jio_snprintf(preimage, len, "%s.preimage", CacheDataStore); 726 727 UseSharedSpaces = false; 728 enable_dumping_static_archive(); 729 SharedArchiveFile = preimage; 730 log_info(cds)("CacheDataStore needs to be updated. Writing %s file", SharedArchiveFile); 731 732 // At VM exit, the module graph may be contaminated with program states. We should rebuild the 733 // module graph when dumping the CDS final image. 734 log_info(cds)("full module graph: disabled when writing CDS preimage"); 735 disable_heap_dumping(); 736 stop_dumping_full_module_graph(); 737 FLAG_SET_ERGO(ArchivePackages, false); 738 FLAG_SET_ERGO(ArchiveProtectionDomains, false); 739 FLAG_SET_ERGO_IF_DEFAULT(RecordTraining, true); 740 _is_dumping_static_archive = true; 741 _is_dumping_preimage_static_archive = true; 742 } 743 } else { 744 // The final image dumping phase -- load the preimage and write the final image file 745 SharedArchiveFile = CDSPreimage; 746 UseSharedSpaces = true; 747 log_info(cds)("Generate CacheDataStore %s from CDSPreimage %s", CacheDataStore, CDSPreimage); 748 // Force -Xbatch for AOT compilation. 749 if (FLAG_SET_CMDLINE(BackgroundCompilation, false) != JVMFlag::SUCCESS) { 750 return false; 751 } 752 RecordTraining = false; // This will be updated inside MetaspaceShared::preload_and_dump() 753 754 FLAG_SET_ERGO_IF_DEFAULT(ReplayTraining, true); 755 // Settings for AOT 756 FLAG_SET_ERGO_IF_DEFAULT(StoreCachedCode, true); 757 if (StoreCachedCode) { 758 // Cannot dump cached code until metadata and heap are dumped. 759 disable_dumping_cached_code(); 760 } 761 _is_dumping_static_archive = true; 762 _is_dumping_final_static_archive = true; 763 } 764 765 return true; 766 } 767 768 bool CDSConfig::is_dumping_classic_static_archive() { 769 return _is_dumping_static_archive && 770 !is_dumping_preimage_static_archive() && 771 !is_dumping_final_static_archive(); 772 } 773 774 bool CDSConfig::is_dumping_preimage_static_archive() { 775 return _is_dumping_preimage_static_archive; 776 } 777 778 bool CDSConfig::is_dumping_preimage_static_archive_with_triggers() { 779 return (!FLAG_IS_DEFAULT(AOTEndTrainingOnMethodEntry)) && is_dumping_preimage_static_archive(); 780 } 781 782 bool CDSConfig::is_dumping_final_static_archive() { 783 return _is_dumping_final_static_archive; 784 } 785 786 bool CDSConfig::allow_only_single_java_thread() { 787 // See comments in JVM_StartThread() 788 return is_dumping_classic_static_archive() || is_dumping_final_static_archive(); 789 } 790 791 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() { 792 if (is_dumping_final_static_archive()) { 793 // No need to regenerate -- the lambda form invokers should have been regenerated 794 // in the preimage archive (if allowed) 795 return false; 796 } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) { 797 // The base archive has aot-linked classes that may have AOT-resolved CP references 798 // that point to the lambda form invokers in the base archive. Such pointers will 799 // be invalid if lambda form invokers are regenerated in the dynamic archive. 800 return false; 801 } else if (CDSConfig::is_dumping_invokedynamic()) { 802 // Work around JDK-8310831, as some methods in lambda form holder classes may not get generated. 803 return false; 804 } else { 805 return is_dumping_archive(); 806 } 807 } 808 809 bool CDSConfig::is_logging_dynamic_proxies() { 810 return ClassListWriter::is_enabled() || is_dumping_preimage_static_archive(); 811 } 812 813 // Preserve all states that were examined used during dumptime verification, such 814 // that the verification result (pass or fail) cannot be changed at runtime. 815 // 816 // For example, if the verification of ik requires that class A must be a subtype of B, 817 // then this relationship between A and B cannot be changed at runtime. I.e., the app 818 // cannot load alternative versions of A and B such that A is not a subtype of B. 819 bool CDSConfig::preserve_all_dumptime_verification_states(const InstanceKlass* ik) { 820 return is_dumping_aot_linked_classes() && SystemDictionaryShared::is_builtin(ik); 821 } 822 823 bool CDSConfig::is_using_archive() { 824 return UseSharedSpaces; 825 } 826 827 bool CDSConfig::is_logging_lambda_form_invokers() { 828 return ClassListWriter::is_enabled() || is_dumping_dynamic_archive() || is_dumping_preimage_static_archive(); 829 } 830 831 void CDSConfig::stop_using_optimized_module_handling() { 832 _is_using_optimized_module_handling = false; 833 _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling() 834 _is_using_full_module_graph = false; // This requires is_using_optimized_module_handling() 835 } 836 837 838 CDSConfig::DumperThreadMark::DumperThreadMark(JavaThread* current) { 839 assert(_dumper_thread == nullptr, "sanity"); 840 _dumper_thread = current; 841 } 842 843 CDSConfig::DumperThreadMark::~DumperThreadMark() { 844 assert(_dumper_thread != nullptr, "sanity"); 845 _dumper_thread = nullptr; 846 } 847 848 bool CDSConfig::current_thread_is_vm_or_dumper() { 849 Thread* t = Thread::current(); 850 return t != nullptr && (t->is_VM_thread() || t == _dumper_thread); 851 } 852 853 const char* CDSConfig::type_of_archive_being_loaded() { 854 if (is_dumping_final_static_archive()) { 855 return "AOT configuration file"; 856 } else if (new_aot_flags_used()) { 857 return "AOT cache"; 858 } else { 859 return "shared archive file"; 860 } 861 } 862 863 const char* CDSConfig::type_of_archive_being_written() { 864 if (is_dumping_preimage_static_archive()) { 865 return "AOT configuration file"; 866 } else if (new_aot_flags_used()) { 867 return "AOT cache"; 868 } else { 869 return "shared archive file"; 870 } 871 } 872 873 // If an incompatible VM options is found, return a text message that explains why 874 static const char* check_options_incompatible_with_dumping_heap() { 875 #if INCLUDE_CDS_JAVA_HEAP 876 if (!UseCompressedClassPointers) { 877 return "UseCompressedClassPointers must be true"; 878 } 879 880 // Almost all GCs support heap region dump, except ZGC (so far). 881 if (UseZGC) { 882 return "ZGC is not supported"; 883 } 884 885 return nullptr; 886 #else 887 return "JVM not configured for writing Java heap objects"; 888 #endif 889 } 890 891 void CDSConfig::log_reasons_for_not_dumping_heap() { 892 const char* reason; 893 894 assert(!is_dumping_heap(), "sanity"); 895 896 if (_disable_heap_dumping) { 897 reason = "Programmatically disabled"; 898 } else { 899 reason = check_options_incompatible_with_dumping_heap(); 900 } 901 902 assert(reason != nullptr, "sanity"); 903 log_info(cds)("Archived java heap is not supported: %s", reason); 904 } 905 906 #if INCLUDE_CDS_JAVA_HEAP 907 bool CDSConfig::are_vm_options_incompatible_with_dumping_heap() { 908 return check_options_incompatible_with_dumping_heap() != nullptr; 909 } 910 911 912 bool CDSConfig::is_dumping_heap() { 913 if (!(is_dumping_classic_static_archive() || is_dumping_final_static_archive()) 914 || are_vm_options_incompatible_with_dumping_heap() 915 || _disable_heap_dumping) { 916 return false; 917 } 918 return true; 919 } 920 921 bool CDSConfig::is_loading_heap() { 922 return ArchiveHeapLoader::is_in_use(); 923 } 924 925 bool CDSConfig::is_using_full_module_graph() { 926 if (ClassLoaderDataShared::is_full_module_graph_loaded()) { 927 return true; 928 } 929 930 if (!_is_using_full_module_graph) { 931 return false; 932 } 933 934 if (is_using_archive() && ArchiveHeapLoader::can_use()) { 935 // Classes used by the archived full module graph are loaded in JVMTI early phase. 936 assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()), 937 "CDS should be disabled if early class hooks are enabled"); 938 return true; 939 } else { 940 _is_using_full_module_graph = false; 941 return false; 942 } 943 } 944 945 void CDSConfig::stop_dumping_full_module_graph(const char* reason) { 946 if (_is_dumping_full_module_graph) { 947 _is_dumping_full_module_graph = false; 948 if (reason != nullptr) { 949 log_info(cds)("full module graph cannot be dumped: %s", reason); 950 } 951 } 952 } 953 954 void CDSConfig::stop_using_full_module_graph(const char* reason) { 955 assert(!ClassLoaderDataShared::is_full_module_graph_loaded(), "you call this function too late!"); 956 if (_is_using_full_module_graph) { 957 _is_using_full_module_graph = false; 958 if (reason != nullptr) { 959 log_info(cds)("full module graph cannot be loaded: %s", reason); 960 } 961 } 962 } 963 964 bool CDSConfig::is_dumping_aot_linked_classes() { 965 if (is_dumping_preimage_static_archive()) { 966 return false; 967 } else if (is_dumping_dynamic_archive()) { 968 return is_using_full_module_graph() && AOTClassLinking; 969 } else if (is_dumping_static_archive()) { 970 return is_dumping_full_module_graph() && AOTClassLinking; 971 } else { 972 return false; 973 } 974 } 975 976 bool CDSConfig::is_using_aot_linked_classes() { 977 if (is_dumping_final_static_archive()) { 978 // We assume that the final image is being dumped with the exact same module graph as the training run, 979 // so all aot-linked classes can be loaded. 980 return _has_aot_linked_classes; 981 } 982 // Make sure we have the exact same module graph as in the assembly phase, or else 983 // some aot-linked classes may not be visible so cannot be loaded. 984 return is_using_full_module_graph() && _has_aot_linked_classes; 985 } 986 987 bool CDSConfig::is_dumping_dynamic_proxies() { 988 return is_dumping_full_module_graph() && is_dumping_invokedynamic() && ArchiveDynamicProxies; 989 } 990 991 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) { 992 _has_aot_linked_classes |= has_aot_linked_classes; 993 } 994 995 bool CDSConfig::is_initing_classes_at_dump_time() { 996 return is_dumping_heap() && is_dumping_aot_linked_classes(); 997 } 998 999 bool CDSConfig::is_dumping_invokedynamic() { 1000 // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap 1001 // objects used by the archive indy callsites may be replaced at runtime. 1002 return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap(); 1003 } 1004 1005 bool CDSConfig::is_dumping_packages() { 1006 return ArchivePackages && is_dumping_heap(); 1007 } 1008 1009 bool CDSConfig::is_loading_packages() { 1010 return UseSharedSpaces && is_using_full_module_graph() && _is_loading_packages; 1011 } 1012 1013 bool CDSConfig::is_dumping_protection_domains() { 1014 if (_is_security_manager_allowed) { 1015 // For sanity, don't archive PDs. TODO: can this be relaxed? 1016 return false; 1017 } 1018 // Archived PDs for the modules will reference their java.lang.Module, which must 1019 // also be archived. 1020 return ArchiveProtectionDomains && is_dumping_full_module_graph(); 1021 } 1022 1023 bool CDSConfig::is_loading_protection_domains() { 1024 if (_is_security_manager_allowed) { 1025 // For sanity, don't used any archived PDs. TODO: can this be relaxed? 1026 return false; 1027 } 1028 return UseSharedSpaces && is_using_full_module_graph() && _is_loading_protection_domains; 1029 } 1030 1031 bool CDSConfig::is_dumping_reflection_data() { 1032 // reflection data use LambdaForm classes 1033 return ArchiveReflectionData && is_dumping_invokedynamic(); 1034 } 1035 1036 bool CDSConfig::is_loading_invokedynamic() { 1037 return UseSharedSpaces && is_using_full_module_graph() && _has_archived_invokedynamic; 1038 } 1039 1040 #endif // INCLUDE_CDS_JAVA_HEAP 1041 1042 // This is allowed by default. We disable it only in the final image dump before the 1043 // metadata and heap are dumped. 1044 static bool _is_dumping_cached_code = true; 1045 1046 bool CDSConfig::is_dumping_cached_code() { 1047 return _is_dumping_cached_code; 1048 } 1049 1050 void CDSConfig::disable_dumping_cached_code() { 1051 _is_dumping_cached_code = false; 1052 } 1053 1054 void CDSConfig::enable_dumping_cached_code() { 1055 _is_dumping_cached_code = true; 1056 } 1057 1058 bool CDSConfig::is_dumping_adapters() { 1059 return (ArchiveAdapters && is_dumping_final_static_archive()); 1060 } 1061 1062 bool CDSConfig::is_experimental_leyden_workflow() { 1063 return CacheDataStore != nullptr || CDSPreimage != nullptr; 1064 }