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