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