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