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/aotLogging.hpp" 26 #include "cds/aotMapLogger.hpp" 27 #include "cds/archiveHeapLoader.hpp" 28 #include "cds/cdsConfig.hpp" 29 #include "cds/classListWriter.hpp" 30 #include "cds/filemap.hpp" 31 #include "cds/heapShared.hpp" 32 #include "classfile/classLoaderDataShared.hpp" 33 #include "classfile/moduleEntry.hpp" 34 #include "code/aotCodeCache.hpp" 35 #include "include/jvm_io.h" 36 #include "logging/log.hpp" 37 #include "memory/universe.hpp" 38 #include "prims/jvmtiAgentList.hpp" 39 #include "runtime/arguments.hpp" 40 #include "runtime/globals.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::_is_single_command_training = false; 56 bool CDSConfig::_has_temp_aot_config_file = false; 57 bool CDSConfig::_old_cds_flags_used = false; 58 bool CDSConfig::_new_aot_flags_used = false; 59 bool CDSConfig::_disable_heap_dumping = false; 60 bool CDSConfig::_is_at_aot_safepoint = false; 61 62 bool CDSConfig::_module_patching_disables_cds = false; 63 bool CDSConfig::_java_base_module_patching_disables_cds = false; 64 65 const char* CDSConfig::_default_archive_path = nullptr; 66 const char* CDSConfig::_input_static_archive_path = nullptr; 67 const char* CDSConfig::_input_dynamic_archive_path = nullptr; 68 const char* CDSConfig::_output_archive_path = nullptr; 69 70 JavaThread* CDSConfig::_dumper_thread = nullptr; 71 72 int CDSConfig::get_status() { 73 assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized"); 74 return (is_dumping_archive() ? IS_DUMPING_ARCHIVE : 0) | 75 (is_dumping_method_handles() ? IS_DUMPING_METHOD_HANDLES : 0) | 76 (is_dumping_static_archive() ? IS_DUMPING_STATIC_ARCHIVE : 0) | 77 (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) | 78 (is_using_archive() ? IS_USING_ARCHIVE : 0); 79 } 80 81 DEBUG_ONLY(static bool _cds_ergo_initialize_started = false); 82 83 void CDSConfig::ergo_initialize() { 84 DEBUG_ONLY(_cds_ergo_initialize_started = true); 85 86 if (is_dumping_static_archive() && !is_dumping_final_static_archive()) { 87 // Note: -Xshare and -XX:AOTMode flags are mutually exclusive. 88 // - Classic workflow: -Xshare:on and -Xshare:dump cannot take effect at the same time. 89 // - JEP 483 workflow: -XX:AOTMode:record and -XX:AOTMode=on cannot take effect at the same time. 90 // So we can never come to here with RequireSharedSpaces==true. 91 assert(!RequireSharedSpaces, "sanity"); 92 93 // If dumping the classic archive, or making an AOT training run (dumping a preimage archive), 94 // for sanity, parse all classes from classfiles. 95 // TODO: in the future, if we want to support re-training on top of an existing AOT cache, this 96 // needs to be changed. 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 if (is_dumping_static_archive() || is_using_archive()) { 103 if (new_aot_flags_used()) { 104 ergo_init_aot_paths(); 105 } else { 106 ergo_init_classic_archive_paths(); 107 } 108 } 109 110 if (!is_dumping_heap()) { 111 _is_dumping_full_module_graph = false; 112 } 113 114 AOTMapLogger::ergo_initialize(); 115 } 116 117 const char* CDSConfig::default_archive_path() { 118 // The path depends on UseCompressedOops, etc, which are set by GC ergonomics just 119 // before CDSConfig::ergo_initialize() is called. 120 assert(_cds_ergo_initialize_started, "sanity"); 121 if (_default_archive_path == nullptr) { 122 stringStream tmp; 123 if (is_vm_statically_linked()) { 124 // It's easier to form the path using JAVA_HOME as os::jvm_path 125 // gives the path to the launcher executable on static JDK. 126 const char* subdir = WINDOWS_ONLY("bin") NOT_WINDOWS("lib"); 127 tmp.print("%s%s%s%s%s%sclasses", 128 Arguments::get_java_home(), os::file_separator(), 129 subdir, os::file_separator(), 130 Abstract_VM_Version::vm_variant(), os::file_separator()); 131 } else { 132 // Assume .jsa is in the same directory where libjvm resides on 133 // non-static JDK. 134 char jvm_path[JVM_MAXPATHLEN]; 135 os::jvm_path(jvm_path, sizeof(jvm_path)); 136 char *end = strrchr(jvm_path, *os::file_separator()); 137 if (end != nullptr) *end = '\0'; 138 tmp.print("%s%sclasses", jvm_path, os::file_separator()); 139 } 140 #ifdef _LP64 141 if (!UseCompressedOops) { 142 tmp.print_raw("_nocoops"); 143 } 144 if (UseCompactObjectHeaders) { 145 // Note that generation of xxx_coh.jsa variants require 146 // --enable-cds-archive-coh at build time 147 tmp.print_raw("_coh"); 148 } 149 #endif 150 if (is_valhalla_preview()) { 151 tmp.print_raw("_valhalla"); 152 } 153 tmp.print_raw(".jsa"); 154 _default_archive_path = os::strdup(tmp.base()); 155 } 156 return _default_archive_path; 157 } 158 159 int CDSConfig::num_archive_paths(const char* path_spec) { 160 if (path_spec == nullptr) { 161 return 0; 162 } 163 int npaths = 1; 164 char* p = (char*)path_spec; 165 while (*p != '\0') { 166 if (*p == os::path_separator()[0]) { 167 npaths++; 168 } 169 p++; 170 } 171 return npaths; 172 } 173 174 void CDSConfig::extract_archive_paths(const char* archive_path, 175 const char** base_archive_path, 176 const char** top_archive_path) { 177 char* begin_ptr = (char*)archive_path; 178 char* end_ptr = strchr((char*)archive_path, os::path_separator()[0]); 179 if (end_ptr == nullptr || end_ptr == begin_ptr) { 180 vm_exit_during_initialization("Base archive was not specified", archive_path); 181 } 182 size_t len = end_ptr - begin_ptr; 183 char* cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal); 184 strncpy(cur_path, begin_ptr, len); 185 cur_path[len] = '\0'; 186 *base_archive_path = cur_path; 187 188 begin_ptr = ++end_ptr; 189 if (*begin_ptr == '\0') { 190 vm_exit_during_initialization("Top archive was not specified", archive_path); 191 } 192 end_ptr = strchr(begin_ptr, '\0'); 193 assert(end_ptr != nullptr, "sanity"); 194 len = end_ptr - begin_ptr; 195 cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal); 196 strncpy(cur_path, begin_ptr, len + 1); 197 *top_archive_path = cur_path; 198 } 199 200 void CDSConfig::ergo_init_classic_archive_paths() { 201 assert(_cds_ergo_initialize_started, "sanity"); 202 if (ArchiveClassesAtExit != nullptr) { 203 assert(!RecordDynamicDumpInfo, "already checked"); 204 if (is_dumping_static_archive()) { 205 vm_exit_during_initialization("-XX:ArchiveClassesAtExit cannot be used with -Xshare:dump"); 206 } 207 check_unsupported_dumping_module_options(); 208 209 if (os::same_files(default_archive_path(), ArchiveClassesAtExit)) { 210 vm_exit_during_initialization( 211 "Cannot specify the default CDS archive for -XX:ArchiveClassesAtExit", default_archive_path()); 212 } 213 } 214 215 if (SharedArchiveFile == nullptr) { 216 _input_static_archive_path = default_archive_path(); 217 if (is_dumping_static_archive()) { 218 _output_archive_path = _input_static_archive_path; 219 } 220 } else { 221 int num_archives = num_archive_paths(SharedArchiveFile); 222 assert(num_archives > 0, "must be"); 223 224 if (is_dumping_archive() && num_archives > 1) { 225 vm_exit_during_initialization( 226 "Cannot have more than 1 archive file specified in -XX:SharedArchiveFile during CDS dumping"); 227 } 228 229 if (is_dumping_static_archive()) { 230 assert(num_archives == 1, "just checked above"); 231 // Static dump is simple: only one archive is allowed in SharedArchiveFile. This file 232 // will be overwritten regardless of its contents 233 _output_archive_path = SharedArchiveFile; 234 } else { 235 // SharedArchiveFile may specify one or two files. In case (c), the path for base.jsa 236 // is read from top.jsa 237 // (a) 1 file: -XX:SharedArchiveFile=base.jsa 238 // (b) 2 files: -XX:SharedArchiveFile=base.jsa:top.jsa 239 // (c) 2 files: -XX:SharedArchiveFile=top.jsa 240 // 241 // However, if either RecordDynamicDumpInfo or ArchiveClassesAtExit is used, we do not 242 // allow cases (b) and (c). Case (b) is already checked above. 243 244 if (num_archives > 2) { 245 vm_exit_during_initialization( 246 "Cannot have more than 2 archive files specified in the -XX:SharedArchiveFile option"); 247 } 248 249 if (num_archives == 1) { 250 const char* base_archive_path = nullptr; 251 bool success = 252 FileMapInfo::get_base_archive_name_from_header(SharedArchiveFile, &base_archive_path); 253 if (!success) { 254 // If +AutoCreateSharedArchive and the specified shared archive does not exist, 255 // regenerate the dynamic archive base on default archive. 256 if (AutoCreateSharedArchive && !os::file_exists(SharedArchiveFile)) { 257 enable_dumping_dynamic_archive(SharedArchiveFile); 258 FLAG_SET_ERGO(ArchiveClassesAtExit, SharedArchiveFile); 259 _input_static_archive_path = default_archive_path(); 260 FLAG_SET_ERGO(SharedArchiveFile, nullptr); 261 } else { 262 if (AutoCreateSharedArchive) { 263 warning("-XX:+AutoCreateSharedArchive is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."); 264 AutoCreateSharedArchive = false; 265 } 266 aot_log_error(aot)("Not a valid %s (%s)", type_of_archive_being_loaded(), SharedArchiveFile); 267 Arguments::no_shared_spaces("invalid archive"); 268 } 269 } else if (base_archive_path == nullptr) { 270 // User has specified a single archive, which is a static archive. 271 _input_static_archive_path = SharedArchiveFile; 272 } else { 273 // User has specified a single archive, which is a dynamic archive. 274 _input_dynamic_archive_path = SharedArchiveFile; 275 _input_static_archive_path = base_archive_path; // has been c-heap allocated. 276 } 277 } else { 278 extract_archive_paths(SharedArchiveFile, 279 &_input_static_archive_path, &_input_dynamic_archive_path); 280 if (_input_static_archive_path == nullptr) { 281 assert(_input_dynamic_archive_path == nullptr, "must be"); 282 Arguments::no_shared_spaces("invalid archive"); 283 } 284 } 285 286 if (_input_dynamic_archive_path != nullptr) { 287 // Check for case (c) 288 if (RecordDynamicDumpInfo) { 289 vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile", 290 SharedArchiveFile); 291 } 292 if (ArchiveClassesAtExit != nullptr) { 293 vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile", 294 SharedArchiveFile); 295 } 296 } 297 298 if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) { 299 vm_exit_during_initialization( 300 "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit", 301 SharedArchiveFile); 302 } 303 } 304 } 305 } 306 307 void CDSConfig::check_internal_module_property(const char* key, const char* value) { 308 if (Arguments::is_incompatible_cds_internal_module_property(key) && !Arguments::patching_migrated_classes(key, value)) { 309 stop_using_optimized_module_handling(); 310 aot_log_info(aot)("optimized module handling: disabled due to incompatible property: %s=%s", key, value); 311 } 312 } 313 314 void CDSConfig::check_incompatible_property(const char* key, const char* value) { 315 static const char* incompatible_properties[] = { 316 "java.system.class.loader", 317 "jdk.module.showModuleResolution", 318 "jdk.module.validation" 319 }; 320 321 for (const char* property : incompatible_properties) { 322 if (strcmp(key, property) == 0) { 323 stop_dumping_full_module_graph(); 324 stop_using_full_module_graph(); 325 aot_log_info(aot)("full module graph: disabled due to incompatible property: %s=%s", key, value); 326 break; 327 } 328 } 329 330 } 331 332 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS. 333 static const char* find_any_unsupported_module_option() { 334 // Note that arguments.cpp has translated the command-line options into properties. If we find an 335 // unsupported property, translate it back to its command-line option for better error reporting. 336 337 // The following properties are checked by Arguments::is_internal_module_property() and cannot be 338 // directly specified in the command-line. 339 static const char* unsupported_module_properties[] = { 340 "jdk.module.limitmods", 341 "jdk.module.upgrade.path" 342 }; 343 static const char* unsupported_module_options[] = { 344 "--limit-modules", 345 "--upgrade-module-path" 346 }; 347 348 assert(ARRAY_SIZE(unsupported_module_properties) == ARRAY_SIZE(unsupported_module_options), "must be"); 349 SystemProperty* sp = Arguments::system_properties(); 350 while (sp != nullptr) { 351 for (uint i = 0; i < ARRAY_SIZE(unsupported_module_properties); i++) { 352 if (strcmp(sp->key(), unsupported_module_properties[i]) == 0) { 353 return unsupported_module_options[i]; 354 } 355 } 356 sp = sp->next(); 357 } 358 359 return nullptr; // not found 360 } 361 362 void CDSConfig::check_unsupported_dumping_module_options() { 363 assert(is_dumping_archive(), "this function is only used with CDS dump time"); 364 const char* option = find_any_unsupported_module_option(); 365 if (option != nullptr) { 366 vm_exit_during_initialization("Cannot use the following option when dumping the shared archive", option); 367 } 368 369 if (module_patching_disables_cds()) { 370 vm_exit_during_initialization( 371 "Cannot use the following option when dumping the shared archive", "--patch-module"); 372 } 373 374 // Check for an exploded module build in use with -Xshare:dump. 375 if (!Arguments::has_jimage()) { 376 vm_exit_during_initialization("Dumping the shared archive is not supported with an exploded module build"); 377 } 378 } 379 380 bool CDSConfig::has_unsupported_runtime_module_options() { 381 assert(is_using_archive(), "this function is only used with -Xshare:{on,auto}"); 382 if (ArchiveClassesAtExit != nullptr) { 383 // dynamic dumping, just return false for now. 384 // check_unsupported_dumping_properties() will be called later to check the same set of 385 // properties, and will exit the VM with the correct error message if the unsupported properties 386 // are used. 387 return false; 388 } 389 const char* option = find_any_unsupported_module_option(); 390 if (option != nullptr) { 391 if (RequireSharedSpaces) { 392 warning("CDS is disabled when the %s option is specified.", option); 393 } else { 394 if (new_aot_flags_used()) { 395 aot_log_warning(aot)("AOT cache is disabled when the %s option is specified.", option); 396 } else { 397 aot_log_info(aot)("CDS is disabled when the %s option is specified.", option); 398 } 399 } 400 return true; 401 } 402 403 if (module_patching_disables_cds()) { 404 if (RequireSharedSpaces) { 405 warning("CDS is disabled when the %s option is specified.", "--patch-module"); 406 } else { 407 log_info(cds)("CDS is disabled when the %s option is specified.", "--patch-module"); 408 } 409 return true; 410 } 411 412 return false; 413 } 414 415 #define CHECK_NEW_FLAG(f) check_new_flag(FLAG_IS_DEFAULT(f), #f) 416 417 void CDSConfig::check_new_flag(bool new_flag_is_default, const char* new_flag_name) { 418 if (old_cds_flags_used() && !new_flag_is_default) { 419 vm_exit_during_initialization(err_msg("Option %s cannot be used at the same time with " 420 "-Xshare:on, -Xshare:auto, -Xshare:off, -Xshare:dump, " 421 "DumpLoadedClassList, SharedClassListFile, or SharedArchiveFile", 422 new_flag_name)); 423 } 424 } 425 426 #define CHECK_SINGLE_PATH(f) check_flag_single_path(#f, f) 427 428 void CDSConfig::check_flag_single_path(const char* flag_name, const char* value) { 429 if (value != nullptr && num_archive_paths(value) != 1) { 430 vm_exit_during_initialization(err_msg("Option %s must specify a single file name", flag_name)); 431 } 432 } 433 434 void CDSConfig::check_aot_flags() { 435 if (!FLAG_IS_DEFAULT(DumpLoadedClassList) || 436 !FLAG_IS_DEFAULT(SharedClassListFile) || 437 !FLAG_IS_DEFAULT(SharedArchiveFile)) { 438 _old_cds_flags_used = true; 439 } 440 441 // "New" AOT flags must not be mixed with "classic" CDS flags such as -Xshare:dump 442 CHECK_NEW_FLAG(AOTCache); 443 CHECK_NEW_FLAG(AOTCacheOutput); 444 CHECK_NEW_FLAG(AOTConfiguration); 445 CHECK_NEW_FLAG(AOTMode); 446 447 CHECK_SINGLE_PATH(AOTCache); 448 CHECK_SINGLE_PATH(AOTCacheOutput); 449 CHECK_SINGLE_PATH(AOTConfiguration); 450 451 if (FLAG_IS_DEFAULT(AOTCache) && AOTAdapterCaching) { 452 log_debug(aot,codecache,init)("AOTCache is not specified - AOTAdapterCaching is ignored"); 453 } 454 if (FLAG_IS_DEFAULT(AOTCache) && AOTStubCaching) { 455 log_debug(aot,codecache,init)("AOTCache is not specified - AOTStubCaching is ignored"); 456 } 457 458 bool has_cache = !FLAG_IS_DEFAULT(AOTCache); 459 bool has_cache_output = !FLAG_IS_DEFAULT(AOTCacheOutput); 460 bool has_config = !FLAG_IS_DEFAULT(AOTConfiguration); 461 bool has_mode = !FLAG_IS_DEFAULT(AOTMode); 462 463 if (!has_cache && !has_cache_output && !has_config && !has_mode) { 464 // AOT flags are not used. Use classic CDS workflow 465 return; 466 } 467 468 if (has_cache && has_cache_output) { 469 vm_exit_during_initialization("Only one of AOTCache or AOTCacheOutput can be specified"); 470 } 471 472 if (!has_cache && (!has_mode || strcmp(AOTMode, "auto") == 0)) { 473 if (has_cache_output) { 474 // If AOTCacheOutput has been set, effective mode is "record". 475 // Default value for AOTConfiguration, if necessary, will be assigned in check_aotmode_record(). 476 log_info(aot)("Selected AOTMode=record because AOTCacheOutput is specified"); 477 FLAG_SET_ERGO(AOTMode, "record"); 478 } 479 } 480 481 // At least one AOT flag has been used 482 _new_aot_flags_used = true; 483 484 if (FLAG_IS_DEFAULT(AOTMode) || strcmp(AOTMode, "auto") == 0 || strcmp(AOTMode, "on") == 0) { 485 check_aotmode_auto_or_on(); 486 } else if (strcmp(AOTMode, "off") == 0) { 487 check_aotmode_off(); 488 } else if (strcmp(AOTMode, "record") == 0) { 489 check_aotmode_record(); 490 } else { 491 assert(strcmp(AOTMode, "create") == 0, "checked by AOTModeConstraintFunc"); 492 check_aotmode_create(); 493 } 494 495 // This is an old flag used by CDS regression testing only. It doesn't apply 496 // to the AOT workflow. 497 FLAG_SET_ERGO(AllowArchivingWithJavaAgent, false); 498 } 499 500 void CDSConfig::check_aotmode_off() { 501 UseSharedSpaces = false; 502 RequireSharedSpaces = false; 503 } 504 505 void CDSConfig::check_aotmode_auto_or_on() { 506 if (!FLAG_IS_DEFAULT(AOTConfiguration)) { 507 vm_exit_during_initialization(err_msg("AOTConfiguration can only be used with when AOTMode is record or create (selected AOTMode = %s)", 508 FLAG_IS_DEFAULT(AOTMode) ? "auto" : AOTMode)); 509 } 510 511 UseSharedSpaces = true; 512 if (FLAG_IS_DEFAULT(AOTMode) || (strcmp(AOTMode, "auto") == 0)) { 513 RequireSharedSpaces = false; 514 } else { 515 assert(strcmp(AOTMode, "on") == 0, "already checked"); 516 RequireSharedSpaces = true; 517 } 518 } 519 520 // %p substitution in AOTCache, AOTCacheOutput and AOTCacheConfiguration 521 static void substitute_aot_filename(JVMFlagsEnum flag_enum) { 522 JVMFlag* flag = JVMFlag::flag_from_enum(flag_enum); 523 const char* filename = flag->read<const char*>(); 524 assert(filename != nullptr, "must not have default value"); 525 526 // For simplicity, we don't allow %p/%t to be specified twice, because make_log_name() 527 // substitutes only the first occurrence. Otherwise, if we run with 528 // java -XX:AOTCacheOutput=%p%p.aot 529 // it will end up with both the pid of the training process and the assembly process. 530 const char* first_p = strstr(filename, "%p"); 531 if (first_p != nullptr && strstr(first_p + 2, "%p") != nullptr) { 532 vm_exit_during_initialization(err_msg("%s cannot contain more than one %%p", flag->name())); 533 } 534 const char* first_t = strstr(filename, "%t"); 535 if (first_t != nullptr && strstr(first_t + 2, "%t") != nullptr) { 536 vm_exit_during_initialization(err_msg("%s cannot contain more than one %%t", flag->name())); 537 } 538 539 // Note: with single-command training, %p will be the pid of the training process, not the 540 // assembly process. 541 const char* new_filename = make_log_name(filename, nullptr); 542 if (strcmp(filename, new_filename) != 0) { 543 JVMFlag::Error err = JVMFlagAccess::set_ccstr(flag, &new_filename, JVMFlagOrigin::ERGONOMIC); 544 assert(err == JVMFlag::SUCCESS, "must never fail"); 545 } 546 FREE_C_HEAP_ARRAY(char, new_filename); 547 } 548 549 void CDSConfig::check_aotmode_record() { 550 bool has_config = !FLAG_IS_DEFAULT(AOTConfiguration); 551 bool has_output = !FLAG_IS_DEFAULT(AOTCacheOutput); 552 553 if (!has_output && !has_config) { 554 vm_exit_during_initialization("At least one of AOTCacheOutput and AOTConfiguration must be specified when using -XX:AOTMode=record"); 555 } 556 557 if (has_output) { 558 _is_single_command_training = true; 559 substitute_aot_filename(FLAG_MEMBER_ENUM(AOTCacheOutput)); 560 if (!has_config) { 561 // Too early; can't use resource allocation yet. 562 size_t len = strlen(AOTCacheOutput) + 10; 563 char* temp = AllocateHeap(len, mtArguments); 564 jio_snprintf(temp, len, "%s.config", AOTCacheOutput); 565 FLAG_SET_ERGO(AOTConfiguration, temp); 566 FreeHeap(temp); 567 _has_temp_aot_config_file = true; 568 } 569 } 570 571 if (!FLAG_IS_DEFAULT(AOTCache)) { 572 vm_exit_during_initialization("AOTCache must not be specified when using -XX:AOTMode=record"); 573 } 574 575 substitute_aot_filename(FLAG_MEMBER_ENUM(AOTConfiguration)); 576 577 UseSharedSpaces = false; 578 RequireSharedSpaces = false; 579 _is_dumping_static_archive = true; 580 _is_dumping_preimage_static_archive = true; 581 582 // At VM exit, the module graph may be contaminated with program states. 583 // We will rebuild the module graph when dumping the CDS final image. 584 disable_heap_dumping(); 585 } 586 587 void CDSConfig::check_aotmode_create() { 588 if (FLAG_IS_DEFAULT(AOTConfiguration)) { 589 vm_exit_during_initialization("AOTConfiguration must be specified when using -XX:AOTMode=create"); 590 } 591 592 bool has_cache = !FLAG_IS_DEFAULT(AOTCache); 593 bool has_cache_output = !FLAG_IS_DEFAULT(AOTCacheOutput); 594 595 assert(!(has_cache && has_cache_output), "already checked"); 596 597 if (!has_cache && !has_cache_output) { 598 vm_exit_during_initialization("AOTCache or AOTCacheOutput must be specified when using -XX:AOTMode=create"); 599 } 600 601 if (!has_cache) { 602 precond(has_cache_output); 603 FLAG_SET_ERGO(AOTCache, AOTCacheOutput); 604 } 605 // No need to check for (!has_cache_output), as we don't look at AOTCacheOutput after here. 606 607 substitute_aot_filename(FLAG_MEMBER_ENUM(AOTCache)); 608 609 _is_dumping_final_static_archive = true; 610 UseSharedSpaces = true; 611 RequireSharedSpaces = true; 612 613 if (!FileMapInfo::is_preimage_static_archive(AOTConfiguration)) { 614 vm_exit_during_initialization("Must be a valid AOT configuration generated by the current JVM", AOTConfiguration); 615 } 616 617 CDSConfig::enable_dumping_static_archive(); 618 619 // We don't load any agents in the assembly phase, so we can ensure that the agents 620 // cannot affect the contents of the AOT cache. E.g., we don't want the agents to 621 // redefine any cached classes. We also don't want the agents to modify heap objects that 622 // are cached. 623 // 624 // Since application is not executed in the assembly phase, there's no need to load 625 // the agents anyway -- no one will notice that the agents are not loaded. 626 log_info(aot)("Disabled all JVMTI agents during -XX:AOTMode=create"); 627 JvmtiAgentList::disable_agent_list(); 628 } 629 630 void CDSConfig::ergo_init_aot_paths() { 631 assert(_cds_ergo_initialize_started, "sanity"); 632 if (is_dumping_static_archive()) { 633 if (is_dumping_preimage_static_archive()) { 634 _output_archive_path = AOTConfiguration; 635 } else { 636 assert(is_dumping_final_static_archive(), "must be"); 637 _input_static_archive_path = AOTConfiguration; 638 _output_archive_path = AOTCache; 639 } 640 } else if (is_using_archive()) { 641 if (FLAG_IS_DEFAULT(AOTCache)) { 642 // Only -XX:AOTMode={auto,on} is specified 643 _input_static_archive_path = default_archive_path(); 644 } else { 645 _input_static_archive_path = AOTCache; 646 } 647 } 648 } 649 650 bool CDSConfig::check_vm_args_consistency(bool mode_flag_cmd_line) { 651 assert(!_cds_ergo_initialize_started, "This is called earlier than CDSConfig::ergo_initialize()"); 652 653 check_aot_flags(); 654 655 if (!FLAG_IS_DEFAULT(AOTMode)) { 656 // Using any form of the new AOTMode switch enables enhanced optimizations. 657 FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true); 658 } 659 660 setup_compiler_args(); 661 662 if (AOTClassLinking) { 663 // If AOTClassLinking is specified, enable all AOT optimizations by default. 664 FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true); 665 } else { 666 // AOTInvokeDynamicLinking depends on AOTClassLinking. 667 FLAG_SET_ERGO(AOTInvokeDynamicLinking, false); 668 } 669 670 if (is_dumping_static_archive()) { 671 if (is_dumping_preimage_static_archive()) { 672 // Don't tweak execution mode during AOT training run 673 } else if (is_dumping_final_static_archive()) { 674 if (Arguments::mode() == Arguments::_comp) { 675 // AOT assembly phase submits the non-blocking compilation requests 676 // for methods collected during training run, then waits for all compilations 677 // to complete. With -Xcomp, we block for each compilation request, which is 678 // counter-productive. Switching back to mixed mode improves testing time 679 // with AOT and -Xcomp. 680 Arguments::set_mode_flags(Arguments::_mixed); 681 } 682 } else if (!mode_flag_cmd_line) { 683 // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive. 684 // 685 // If your classlist is large and you don't care about deterministic dumping, you can use 686 // -Xshare:dump -Xmixed to improve dumping speed. 687 Arguments::set_mode_flags(Arguments::_int); 688 } else if (Arguments::mode() == Arguments::_comp) { 689 // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of 690 // Java code, so there's not much benefit in running -Xcomp. 691 aot_log_info(aot)("reduced -Xcomp to -Xmixed for static dumping"); 692 Arguments::set_mode_flags(Arguments::_mixed); 693 } 694 695 // String deduplication may cause CDS to iterate the strings in different order from one 696 // run to another which resulting in non-determinstic CDS archives. 697 // Disable UseStringDeduplication while dumping CDS archive. 698 UseStringDeduplication = false; 699 } 700 701 // RecordDynamicDumpInfo is not compatible with ArchiveClassesAtExit 702 if (ArchiveClassesAtExit != nullptr && RecordDynamicDumpInfo) { 703 jio_fprintf(defaultStream::output_stream(), 704 "-XX:+RecordDynamicDumpInfo cannot be used with -XX:ArchiveClassesAtExit.\n"); 705 return false; 706 } 707 708 if (ArchiveClassesAtExit == nullptr && !RecordDynamicDumpInfo) { 709 disable_dumping_dynamic_archive(); 710 } else { 711 enable_dumping_dynamic_archive(ArchiveClassesAtExit); 712 } 713 714 if (AutoCreateSharedArchive) { 715 if (SharedArchiveFile == nullptr) { 716 aot_log_warning(aot)("-XX:+AutoCreateSharedArchive requires -XX:SharedArchiveFile"); 717 return false; 718 } 719 if (ArchiveClassesAtExit != nullptr) { 720 aot_log_warning(aot)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit"); 721 return false; 722 } 723 } 724 725 if (is_using_archive() && java_base_module_patching_disables_cds() && module_patching_disables_cds()) { 726 Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched."); 727 } 728 if (is_using_archive() && has_unsupported_runtime_module_options()) { 729 UseSharedSpaces = false; 730 } 731 732 if (is_dumping_archive()) { 733 // Always verify non-system classes during CDS dump 734 if (!BytecodeVerificationRemote) { 735 BytecodeVerificationRemote = true; 736 aot_log_info(aot)("All non-system classes will be verified (-Xverify:remote) during CDS dump time."); 737 } 738 } 739 740 if (is_dumping_classic_static_archive() && AOTClassLinking) { 741 if (JvmtiAgentList::disable_agent_list()) { 742 FLAG_SET_ERGO(AllowArchivingWithJavaAgent, false); 743 log_warning(cds)("Disabled all JVMTI agents with -Xshare:dump -XX:+AOTClassLinking"); 744 } 745 } 746 747 return true; 748 } 749 750 void CDSConfig::setup_compiler_args() { 751 // AOT profiles and AOT-compiled code are supported only in the JEP 483 workflow. 752 bool can_dump_profile_and_compiled_code = AOTClassLinking && new_aot_flags_used(); 753 754 if (is_dumping_preimage_static_archive() && can_dump_profile_and_compiled_code) { 755 // JEP 483 workflow -- training 756 FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true); 757 FLAG_SET_ERGO(AOTReplayTraining, false); 758 AOTCodeCache::disable_caching(); // No AOT code generation during training run 759 } else if (is_dumping_final_static_archive() && can_dump_profile_and_compiled_code) { 760 // JEP 483 workflow -- assembly 761 FLAG_SET_ERGO(AOTRecordTraining, false); 762 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true); 763 AOTCodeCache::enable_caching(); // Generate AOT code during assembly phase. 764 disable_dumping_aot_code(); // Don't dump AOT code until metadata and heap are dumped. 765 } else if (is_using_archive() && new_aot_flags_used()) { 766 // JEP 483 workflow -- production 767 FLAG_SET_ERGO(AOTRecordTraining, false); 768 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true); 769 AOTCodeCache::enable_caching(); 770 } else { 771 FLAG_SET_ERGO(AOTReplayTraining, false); 772 FLAG_SET_ERGO(AOTRecordTraining, false); 773 AOTCodeCache::disable_caching(); 774 } 775 } 776 777 void CDSConfig::prepare_for_dumping() { 778 assert(CDSConfig::is_dumping_archive(), "sanity"); 779 780 if (is_dumping_dynamic_archive() && AOTClassLinking) { 781 if (FLAG_IS_CMDLINE(AOTClassLinking)) { 782 log_warning(cds)("AOTClassLinking is not supported for dynamic CDS archive"); 783 } 784 FLAG_SET_ERGO(AOTClassLinking, false); 785 } 786 787 if (is_dumping_dynamic_archive() && !is_using_archive()) { 788 assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives"); 789 790 // This could happen if SharedArchiveFile has failed to load: 791 // - -Xshare:off was specified 792 // - SharedArchiveFile points to an non-existent file. 793 // - SharedArchiveFile points to an archive that has failed CRC check 794 // - SharedArchiveFile is not specified and the VM doesn't have a compatible default archive 795 796 #define __THEMSG " is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info." 797 if (RecordDynamicDumpInfo) { 798 aot_log_error(aot)("-XX:+RecordDynamicDumpInfo%s", __THEMSG); 799 AOTMetaspace::unrecoverable_loading_error(); 800 } else { 801 assert(ArchiveClassesAtExit != nullptr, "sanity"); 802 aot_log_warning(aot)("-XX:ArchiveClassesAtExit" __THEMSG); 803 } 804 #undef __THEMSG 805 disable_dumping_dynamic_archive(); 806 return; 807 } 808 809 check_unsupported_dumping_module_options(); 810 } 811 812 bool CDSConfig::is_dumping_classic_static_archive() { 813 return _is_dumping_static_archive && 814 !is_dumping_preimage_static_archive() && 815 !is_dumping_final_static_archive(); 816 } 817 818 bool CDSConfig::is_dumping_preimage_static_archive() { 819 return _is_dumping_preimage_static_archive; 820 } 821 822 bool CDSConfig::is_dumping_final_static_archive() { 823 return _is_dumping_final_static_archive; 824 } 825 826 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) { 827 _is_dumping_dynamic_archive = true; 828 if (output_path == nullptr) { 829 // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo 830 // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual 831 // output path. 832 _output_archive_path = nullptr; 833 } else { 834 _output_archive_path = os::strdup_check_oom(output_path, mtArguments); 835 } 836 } 837 838 bool CDSConfig::allow_only_single_java_thread() { 839 // See comments in JVM_StartThread() 840 return is_dumping_classic_static_archive() || is_dumping_final_static_archive(); 841 } 842 843 bool CDSConfig::is_using_archive() { 844 return UseSharedSpaces; 845 } 846 847 bool CDSConfig::is_using_only_default_archive() { 848 return is_using_archive() && 849 input_static_archive_path() != nullptr && 850 default_archive_path() != nullptr && 851 strcmp(input_static_archive_path(), default_archive_path()) == 0 && 852 input_dynamic_archive_path() == nullptr; 853 } 854 855 bool CDSConfig::is_logging_lambda_form_invokers() { 856 return ClassListWriter::is_enabled() || is_dumping_dynamic_archive(); 857 } 858 859 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() { 860 if (is_dumping_final_static_archive()) { 861 // No need to regenerate -- the lambda form invokers should have been regenerated 862 // in the preimage archive (if allowed) 863 return false; 864 } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) { 865 // The base archive has aot-linked classes that may have AOT-resolved CP references 866 // that point to the lambda form invokers in the base archive. Such pointers will 867 // be invalid if lambda form invokers are regenerated in the dynamic archive. 868 return false; 869 } else { 870 return is_dumping_archive(); 871 } 872 } 873 874 void CDSConfig::stop_using_optimized_module_handling() { 875 _is_using_optimized_module_handling = false; 876 _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling() 877 _is_using_full_module_graph = false; // This requires is_using_optimized_module_handling() 878 } 879 880 881 CDSConfig::DumperThreadMark::DumperThreadMark(JavaThread* current) { 882 assert(_dumper_thread == nullptr, "sanity"); 883 _dumper_thread = current; 884 } 885 886 CDSConfig::DumperThreadMark::~DumperThreadMark() { 887 assert(_dumper_thread != nullptr, "sanity"); 888 _dumper_thread = nullptr; 889 } 890 891 bool CDSConfig::current_thread_is_vm_or_dumper() { 892 Thread* t = Thread::current(); 893 return t->is_VM_thread() || t == _dumper_thread; 894 } 895 896 bool CDSConfig::current_thread_is_dumper() { 897 return Thread::current() == _dumper_thread; 898 } 899 900 const char* CDSConfig::type_of_archive_being_loaded() { 901 if (is_dumping_final_static_archive()) { 902 return "AOT configuration file"; 903 } else if (new_aot_flags_used()) { 904 return "AOT cache"; 905 } else { 906 return "shared archive file"; 907 } 908 } 909 910 const char* CDSConfig::type_of_archive_being_written() { 911 if (is_dumping_preimage_static_archive()) { 912 return "AOT configuration file"; 913 } else if (new_aot_flags_used()) { 914 return "AOT cache"; 915 } else { 916 return "shared archive file"; 917 } 918 } 919 920 // If an incompatible VM options is found, return a text message that explains why 921 static const char* check_options_incompatible_with_dumping_heap() { 922 #if INCLUDE_CDS_JAVA_HEAP 923 if (!UseCompressedClassPointers) { 924 return "UseCompressedClassPointers must be true"; 925 } 926 927 // Almost all GCs support heap region dump, except ZGC (so far). 928 if (UseZGC) { 929 return "ZGC is not supported"; 930 } 931 932 return nullptr; 933 #else 934 return "JVM not configured for writing Java heap objects"; 935 #endif 936 } 937 938 void CDSConfig::log_reasons_for_not_dumping_heap() { 939 const char* reason; 940 941 assert(!is_dumping_heap(), "sanity"); 942 943 if (_disable_heap_dumping) { 944 reason = "Programmatically disabled"; 945 } else { 946 reason = check_options_incompatible_with_dumping_heap(); 947 } 948 949 assert(reason != nullptr, "sanity"); 950 aot_log_info(aot)("Archived java heap is not supported: %s", reason); 951 } 952 953 // This is *Legacy* optimization for lambdas before JEP 483. May be removed in the future. 954 bool CDSConfig::is_dumping_lambdas_in_legacy_mode() { 955 return !is_dumping_method_handles(); 956 } 957 958 bool CDSConfig::is_preserving_verification_constraints() { 959 // Verification dependencies are classes used in assignability checks by the 960 // bytecode verifier. In the following example, the verification dependencies 961 // for X are A and B. 962 // 963 // class X { 964 // A getA() { return new B(); } 965 // } 966 // 967 // With the AOT cache, we can ensure that all the verification dependencies 968 // (A and B in the above example) are unconditionally loaded during the bootstrap 969 // of the production run. This means that if a class was successfully verified 970 // in the assembly phase, all of the verifier's assignability checks will remain 971 // valid in the production run, so we don't need to verify aot-linked classes again. 972 973 if (is_dumping_preimage_static_archive()) { // writing AOT config 974 return AOTClassLinking; 975 } else if (is_dumping_final_static_archive()) { // writing AOT cache 976 return is_dumping_aot_linked_classes(); 977 } else { 978 // For simplicity, we don't support this optimization with the old CDS workflow. 979 return false; 980 } 981 } 982 983 bool CDSConfig::is_old_class_for_verifier(const InstanceKlass* ik) { 984 return ik->major_version() < 50 /*JAVA_6_VERSION*/; 985 } 986 987 #if INCLUDE_CDS_JAVA_HEAP 988 bool CDSConfig::are_vm_options_incompatible_with_dumping_heap() { 989 return check_options_incompatible_with_dumping_heap() != nullptr; 990 } 991 992 bool CDSConfig::is_dumping_heap() { 993 if (is_valhalla_preview()) { 994 // Not working yet -- e.g., HeapShared::oop_hash() needs to be implemented for value oops 995 return false; 996 } 997 if (!(is_dumping_classic_static_archive() || is_dumping_final_static_archive()) 998 || are_vm_options_incompatible_with_dumping_heap() 999 || _disable_heap_dumping) { 1000 return false; 1001 } 1002 return true; 1003 } 1004 1005 bool CDSConfig::is_loading_heap() { 1006 return ArchiveHeapLoader::is_in_use(); 1007 } 1008 1009 bool CDSConfig::is_using_full_module_graph() { 1010 if (ClassLoaderDataShared::is_full_module_graph_loaded()) { 1011 return true; 1012 } 1013 1014 if (!_is_using_full_module_graph) { 1015 return false; 1016 } 1017 1018 if (is_using_archive() && ArchiveHeapLoader::can_use()) { 1019 // Classes used by the archived full module graph are loaded in JVMTI early phase. 1020 assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()), 1021 "CDS should be disabled if early class hooks are enabled"); 1022 return true; 1023 } else { 1024 _is_using_full_module_graph = false; 1025 return false; 1026 } 1027 } 1028 1029 void CDSConfig::stop_dumping_full_module_graph(const char* reason) { 1030 if (_is_dumping_full_module_graph) { 1031 _is_dumping_full_module_graph = false; 1032 if (reason != nullptr) { 1033 aot_log_info(aot)("full module graph cannot be dumped: %s", reason); 1034 } 1035 } 1036 } 1037 1038 void CDSConfig::stop_using_full_module_graph(const char* reason) { 1039 assert(!ClassLoaderDataShared::is_full_module_graph_loaded(), "you call this function too late!"); 1040 if (_is_using_full_module_graph) { 1041 _is_using_full_module_graph = false; 1042 if (reason != nullptr) { 1043 aot_log_info(aot)("full module graph cannot be loaded: %s", reason); 1044 } 1045 } 1046 } 1047 1048 bool CDSConfig::is_dumping_aot_linked_classes() { 1049 if (is_dumping_classic_static_archive() || is_dumping_final_static_archive()) { 1050 // FMG is required to guarantee that all cached boot/platform/app classes 1051 // are visible in the production run, so they can be unconditionally 1052 // loaded during VM bootstrap. 1053 return is_dumping_full_module_graph() && AOTClassLinking; 1054 } else { 1055 return false; 1056 } 1057 } 1058 1059 bool CDSConfig::is_using_aot_linked_classes() { 1060 // Make sure we have the exact same module graph as in the assembly phase, or else 1061 // some aot-linked classes may not be visible so cannot be loaded. 1062 return is_using_full_module_graph() && _has_aot_linked_classes; 1063 } 1064 1065 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) { 1066 _has_aot_linked_classes |= has_aot_linked_classes; 1067 } 1068 1069 bool CDSConfig::is_initing_classes_at_dump_time() { 1070 return is_dumping_heap() && is_dumping_aot_linked_classes(); 1071 } 1072 1073 bool CDSConfig::is_dumping_invokedynamic() { 1074 // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap 1075 // objects used by the archive indy callsites may be replaced at runtime. 1076 return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap(); 1077 } 1078 1079 // When we are dumping aot-linked classes and we are able to write archived heap objects, we automatically 1080 // enable the archiving of MethodHandles. This will in turn enable the archiving of MethodTypes and hidden 1081 // classes that are used in the implementation of MethodHandles. 1082 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic 1083 // and dynamic proxies. 1084 bool CDSConfig::is_dumping_method_handles() { 1085 return is_initing_classes_at_dump_time(); 1086 } 1087 1088 #endif // INCLUDE_CDS_JAVA_HEAP 1089 1090 // AOT code generation and its archiving is disabled by default. 1091 // We enable it only in the final image dump after the metadata and heap are dumped. 1092 // This affects only JITed code because it may have embedded oops and metadata pointers 1093 // which AOT code encodes as offsets in final CDS archive regions. 1094 1095 static bool _is_dumping_aot_code = false; 1096 1097 bool CDSConfig::is_dumping_aot_code() { 1098 return _is_dumping_aot_code; 1099 } 1100 1101 void CDSConfig::disable_dumping_aot_code() { 1102 _is_dumping_aot_code = false; 1103 } 1104 1105 void CDSConfig::enable_dumping_aot_code() { 1106 _is_dumping_aot_code = true; 1107 } 1108 1109 bool CDSConfig::is_dumping_adapters() { 1110 return (AOTAdapterCaching && is_dumping_final_static_archive()); 1111 }