1 /* 2 * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "cds/archiveHeapLoader.hpp" 27 #include "cds/cdsConfig.hpp" 28 #include "cds/classListWriter.hpp" 29 #include "cds/heapShared.hpp" 30 #include "classfile/classLoaderDataShared.hpp" 31 #include "classfile/moduleEntry.hpp" 32 #include "include/jvm_io.h" 33 #include "logging/log.hpp" 34 #include "memory/universe.hpp" 35 #include "runtime/arguments.hpp" 36 #include "runtime/globals.hpp" 37 #include "runtime/java.hpp" 38 #include "utilities/defaultStream.hpp" 39 40 bool CDSConfig::_is_dumping_static_archive = false; 41 bool CDSConfig::_is_dumping_dynamic_archive = false; 42 bool CDSConfig::_is_using_optimized_module_handling = true; 43 bool CDSConfig::_is_dumping_full_module_graph = true; 44 bool CDSConfig::_is_using_full_module_graph = true; 45 46 bool CDSConfig::_module_patching_disables_cds = false; 47 bool CDSConfig::_java_base_module_patching_disables_cds = false; 48 49 bool CDSConfig::is_valhalla_preview() { 50 return Arguments::enable_preview() && EnableValhalla; 51 } 52 53 54 char* CDSConfig::_default_archive_path = nullptr; 55 char* CDSConfig::_static_archive_path = nullptr; 56 char* CDSConfig::_dynamic_archive_path = nullptr; 57 58 int CDSConfig::get_status() { 59 assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized"); 60 return (is_dumping_archive() ? IS_DUMPING_ARCHIVE : 0) | 61 (is_dumping_static_archive() ? IS_DUMPING_STATIC_ARCHIVE : 0) | 62 (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) | 63 (is_using_archive() ? IS_USING_ARCHIVE : 0); 64 } 65 66 67 void CDSConfig::initialize() { 68 if (is_dumping_static_archive()) { 69 if (RequireSharedSpaces) { 70 warning("Cannot dump shared archive while using shared archive"); 71 } 72 UseSharedSpaces = false; 73 } 74 75 // Initialize shared archive paths which could include both base and dynamic archive paths 76 // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly. 77 // 78 // UseSharedSpaces may be disabled if -XX:SharedArchiveFile is invalid. 79 if (is_dumping_static_archive() || is_using_archive()) { 80 init_shared_archive_paths(); 81 } 82 83 if (!is_dumping_heap()) { 84 _is_dumping_full_module_graph = false; 85 } 86 } 87 88 char* CDSConfig::default_archive_path() { 89 if (_default_archive_path == nullptr) { 90 char jvm_path[JVM_MAXPATHLEN]; 91 os::jvm_path(jvm_path, sizeof(jvm_path)); 92 char *end = strrchr(jvm_path, *os::file_separator()); 93 if (end != nullptr) *end = '\0'; 94 size_t jvm_path_len = strlen(jvm_path); 95 size_t file_sep_len = strlen(os::file_separator()); 96 const size_t len = jvm_path_len + file_sep_len + strlen("classes_nocoops_valhalla.jsa") + 1; 97 _default_archive_path = NEW_C_HEAP_ARRAY(char, len, mtArguments); 98 LP64_ONLY(bool nocoops = !UseCompressedOops); 99 NOT_LP64(bool nocoops = false); 100 bool valhalla = is_valhalla_preview(); 101 jio_snprintf(_default_archive_path, len, "%s%sclasses%s%s.jsa", 102 jvm_path, os::file_separator(), 103 nocoops ? "_nocoops" : "", 104 valhalla ? "_valhalla" : ""); 105 } 106 return _default_archive_path; 107 } 108 109 int CDSConfig::num_archives(const char* archive_path) { 110 if (archive_path == nullptr) { 111 return 0; 112 } 113 int npaths = 1; 114 char* p = (char*)archive_path; 115 while (*p != '\0') { 116 if (*p == os::path_separator()[0]) { 117 npaths++; 118 } 119 p++; 120 } 121 return npaths; 122 } 123 124 void CDSConfig::extract_shared_archive_paths(const char* archive_path, 125 char** base_archive_path, 126 char** top_archive_path) { 127 char* begin_ptr = (char*)archive_path; 128 char* end_ptr = strchr((char*)archive_path, os::path_separator()[0]); 129 if (end_ptr == nullptr || end_ptr == begin_ptr) { 130 vm_exit_during_initialization("Base archive was not specified", archive_path); 131 } 132 size_t len = end_ptr - begin_ptr; 133 char* cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal); 134 strncpy(cur_path, begin_ptr, len); 135 cur_path[len] = '\0'; 136 *base_archive_path = cur_path; 137 138 begin_ptr = ++end_ptr; 139 if (*begin_ptr == '\0') { 140 vm_exit_during_initialization("Top archive was not specified", archive_path); 141 } 142 end_ptr = strchr(begin_ptr, '\0'); 143 assert(end_ptr != nullptr, "sanity"); 144 len = end_ptr - begin_ptr; 145 cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal); 146 strncpy(cur_path, begin_ptr, len + 1); 147 *top_archive_path = cur_path; 148 } 149 150 void CDSConfig::init_shared_archive_paths() { 151 if (ArchiveClassesAtExit != nullptr) { 152 assert(!RecordDynamicDumpInfo, "already checked"); 153 if (is_dumping_static_archive()) { 154 vm_exit_during_initialization("-XX:ArchiveClassesAtExit cannot be used with -Xshare:dump"); 155 } 156 check_unsupported_dumping_module_options(); 157 158 if (os::same_files(default_archive_path(), ArchiveClassesAtExit)) { 159 vm_exit_during_initialization( 160 "Cannot specify the default CDS archive for -XX:ArchiveClassesAtExit", default_archive_path()); 161 } 162 } 163 164 if (SharedArchiveFile == nullptr) { 165 _static_archive_path = default_archive_path(); 166 } else { 167 int archives = num_archives(SharedArchiveFile); 168 assert(archives > 0, "must be"); 169 170 if (is_dumping_archive() && archives > 1) { 171 vm_exit_during_initialization( 172 "Cannot have more than 1 archive file specified in -XX:SharedArchiveFile during CDS dumping"); 173 } 174 175 if (is_dumping_static_archive()) { 176 assert(archives == 1, "must be"); 177 // Static dump is simple: only one archive is allowed in SharedArchiveFile. This file 178 // will be overwritten no matter regardless of its contents 179 _static_archive_path = os::strdup_check_oom(SharedArchiveFile, mtArguments); 180 } else { 181 // SharedArchiveFile may specify one or two files. In case (c), the path for base.jsa 182 // is read from top.jsa 183 // (a) 1 file: -XX:SharedArchiveFile=base.jsa 184 // (b) 2 files: -XX:SharedArchiveFile=base.jsa:top.jsa 185 // (c) 2 files: -XX:SharedArchiveFile=top.jsa 186 // 187 // However, if either RecordDynamicDumpInfo or ArchiveClassesAtExit is used, we do not 188 // allow cases (b) and (c). Case (b) is already checked above. 189 190 if (archives > 2) { 191 vm_exit_during_initialization( 192 "Cannot have more than 2 archive files specified in the -XX:SharedArchiveFile option"); 193 } 194 if (archives == 1) { 195 char* base_archive_path = nullptr; 196 bool success = 197 FileMapInfo::get_base_archive_name_from_header(SharedArchiveFile, &base_archive_path); 198 if (!success) { 199 // If +AutoCreateSharedArchive and the specified shared archive does not exist, 200 // regenerate the dynamic archive base on default archive. 201 if (AutoCreateSharedArchive && !os::file_exists(SharedArchiveFile)) { 202 enable_dumping_dynamic_archive(); 203 ArchiveClassesAtExit = const_cast<char *>(SharedArchiveFile); 204 _static_archive_path = default_archive_path(); 205 SharedArchiveFile = nullptr; 206 } else { 207 if (AutoCreateSharedArchive) { 208 warning("-XX:+AutoCreateSharedArchive is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."); 209 AutoCreateSharedArchive = false; 210 } 211 Arguments::no_shared_spaces("invalid archive"); 212 } 213 } else if (base_archive_path == nullptr) { 214 // User has specified a single archive, which is a static archive. 215 _static_archive_path = const_cast<char *>(SharedArchiveFile); 216 } else { 217 // User has specified a single archive, which is a dynamic archive. 218 _dynamic_archive_path = const_cast<char *>(SharedArchiveFile); 219 _static_archive_path = base_archive_path; // has been c-heap allocated. 220 } 221 } else { 222 extract_shared_archive_paths((const char*)SharedArchiveFile, 223 &_static_archive_path, &_dynamic_archive_path); 224 if (_static_archive_path == nullptr) { 225 assert(_dynamic_archive_path == nullptr, "must be"); 226 Arguments::no_shared_spaces("invalid archive"); 227 } 228 } 229 230 if (_dynamic_archive_path != nullptr) { 231 // Check for case (c) 232 if (RecordDynamicDumpInfo) { 233 vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile", 234 SharedArchiveFile); 235 } 236 if (ArchiveClassesAtExit != nullptr) { 237 vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile", 238 SharedArchiveFile); 239 } 240 } 241 242 if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) { 243 vm_exit_during_initialization( 244 "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit", 245 SharedArchiveFile); 246 } 247 } 248 } 249 } 250 251 void CDSConfig::check_internal_module_property(const char* key, const char* value) { 252 if (Arguments::is_internal_module_property(key) && !Arguments::is_module_path_property(key)) { 253 stop_using_optimized_module_handling(); 254 log_info(cds)("optimized module handling: disabled due to incompatible property: %s=%s", key, value); 255 } 256 } 257 258 void CDSConfig::check_incompatible_property(const char* key, const char* value) { 259 static const char* incompatible_properties[] = { 260 "java.system.class.loader", 261 "jdk.module.showModuleResolution", 262 "jdk.module.validation" 263 }; 264 265 for (const char* property : incompatible_properties) { 266 if (strcmp(key, property) == 0) { 267 stop_dumping_full_module_graph(); 268 stop_using_full_module_graph(); 269 log_info(cds)("full module graph: disabled due to incompatible property: %s=%s", key, value); 270 break; 271 } 272 } 273 274 } 275 276 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS. 277 static const char* find_any_unsupported_module_option() { 278 // Note that arguments.cpp has translated the command-line options into properties. If we find an 279 // unsupported property, translate it back to its command-line option for better error reporting. 280 281 // The following properties are checked by Arguments::is_internal_module_property() and cannot be 282 // directly specified in the command-line. 283 static const char* unsupported_module_properties[] = { 284 "jdk.module.limitmods", 285 "jdk.module.upgrade.path" 286 }; 287 static const char* unsupported_module_options[] = { 288 "--limit-modules", 289 "--upgrade-module-path" 290 }; 291 292 assert(ARRAY_SIZE(unsupported_module_properties) == ARRAY_SIZE(unsupported_module_options), "must be"); 293 SystemProperty* sp = Arguments::system_properties(); 294 while (sp != nullptr) { 295 for (uint i = 0; i < ARRAY_SIZE(unsupported_module_properties); i++) { 296 if (strcmp(sp->key(), unsupported_module_properties[i]) == 0) { 297 return unsupported_module_options[i]; 298 } 299 } 300 sp = sp->next(); 301 } 302 303 return nullptr; // not found 304 } 305 306 void CDSConfig::check_unsupported_dumping_module_options() { 307 assert(is_dumping_archive(), "this function is only used with CDS dump time"); 308 const char* option = find_any_unsupported_module_option(); 309 if (option != nullptr) { 310 vm_exit_during_initialization("Cannot use the following option when dumping the shared archive", option); 311 } 312 313 if (module_patching_disables_cds()) { 314 vm_exit_during_initialization( 315 "Cannot use the following option when dumping the shared archive", "--patch-module"); 316 } 317 318 // Check for an exploded module build in use with -Xshare:dump. 319 if (!Arguments::has_jimage()) { 320 vm_exit_during_initialization("Dumping the shared archive is not supported with an exploded module build"); 321 } 322 } 323 324 bool CDSConfig::has_unsupported_runtime_module_options() { 325 assert(is_using_archive(), "this function is only used with -Xshare:{on,auto}"); 326 if (ArchiveClassesAtExit != nullptr) { 327 // dynamic dumping, just return false for now. 328 // check_unsupported_dumping_properties() will be called later to check the same set of 329 // properties, and will exit the VM with the correct error message if the unsupported properties 330 // are used. 331 return false; 332 } 333 const char* option = find_any_unsupported_module_option(); 334 if (option != nullptr) { 335 if (RequireSharedSpaces) { 336 warning("CDS is disabled when the %s option is specified.", option); 337 } else { 338 log_info(cds)("CDS is disabled when the %s option is specified.", option); 339 } 340 return true; 341 } 342 343 if (module_patching_disables_cds()) { 344 if (RequireSharedSpaces) { 345 warning("CDS is disabled when the %s option is specified.", "--patch-module"); 346 } else { 347 log_info(cds)("CDS is disabled when the %s option is specified.", "--patch-module"); 348 } 349 return true; 350 } 351 352 return false; 353 } 354 355 bool CDSConfig::check_vm_args_consistency(bool mode_flag_cmd_line) { 356 if (is_dumping_static_archive()) { 357 if (!mode_flag_cmd_line) { 358 // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive. 359 // 360 // If your classlist is large and you don't care about deterministic dumping, you can use 361 // -Xshare:dump -Xmixed to improve dumping speed. 362 Arguments::set_mode_flags(Arguments::_int); 363 } else if (Arguments::mode() == Arguments::_comp) { 364 // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of 365 // Java code, so there's not much benefit in running -Xcomp. 366 log_info(cds)("reduced -Xcomp to -Xmixed for static dumping"); 367 Arguments::set_mode_flags(Arguments::_mixed); 368 } 369 370 // String deduplication may cause CDS to iterate the strings in different order from one 371 // run to another which resulting in non-determinstic CDS archives. 372 // Disable UseStringDeduplication while dumping CDS archive. 373 UseStringDeduplication = false; 374 } 375 376 // RecordDynamicDumpInfo is not compatible with ArchiveClassesAtExit 377 if (ArchiveClassesAtExit != nullptr && RecordDynamicDumpInfo) { 378 jio_fprintf(defaultStream::output_stream(), 379 "-XX:+RecordDynamicDumpInfo cannot be used with -XX:ArchiveClassesAtExit.\n"); 380 return false; 381 } 382 383 if (ArchiveClassesAtExit == nullptr && !RecordDynamicDumpInfo) { 384 disable_dumping_dynamic_archive(); 385 } else { 386 enable_dumping_dynamic_archive(); 387 } 388 389 if (AutoCreateSharedArchive) { 390 if (SharedArchiveFile == nullptr) { 391 log_warning(cds)("-XX:+AutoCreateSharedArchive requires -XX:SharedArchiveFile"); 392 return false; 393 } 394 if (ArchiveClassesAtExit != nullptr) { 395 log_warning(cds)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit"); 396 return false; 397 } 398 } 399 400 if (is_using_archive() && java_base_module_patching_disables_cds() && module_patching_disables_cds()) { 401 Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched."); 402 } 403 if (is_using_archive() && has_unsupported_runtime_module_options()) { 404 UseSharedSpaces = false; 405 } 406 407 if (is_dumping_archive()) { 408 // Always verify non-system classes during CDS dump 409 if (!BytecodeVerificationRemote) { 410 BytecodeVerificationRemote = true; 411 log_info(cds)("All non-system classes will be verified (-Xverify:remote) during CDS dump time."); 412 } 413 } 414 415 return true; 416 } 417 418 bool CDSConfig::is_using_archive() { 419 return UseSharedSpaces; 420 } 421 422 bool CDSConfig::is_logging_lambda_form_invokers() { 423 return ClassListWriter::is_enabled() || is_dumping_dynamic_archive(); 424 } 425 426 void CDSConfig::stop_using_optimized_module_handling() { 427 _is_using_optimized_module_handling = false; 428 _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling() 429 _is_using_full_module_graph = false; // This requires is_using_optimized_module_handling() 430 } 431 432 #if INCLUDE_CDS_JAVA_HEAP 433 bool CDSConfig::is_dumping_heap() { 434 if (is_valhalla_preview()) { 435 // Not working yet -- e.g., HeapShared::oop_hash() needs to be implemented for value oops 436 return false; 437 } 438 // heap dump is not supported in dynamic dump 439 return is_dumping_static_archive() && HeapShared::can_write(); 440 } 441 442 bool CDSConfig::is_using_full_module_graph() { 443 if (ClassLoaderDataShared::is_full_module_graph_loaded()) { 444 return true; 445 } 446 447 if (!_is_using_full_module_graph) { 448 return false; 449 } 450 451 if (is_using_archive() && ArchiveHeapLoader::can_use()) { 452 // Classes used by the archived full module graph are loaded in JVMTI early phase. 453 assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()), 454 "CDS should be disabled if early class hooks are enabled"); 455 return true; 456 } else { 457 _is_using_full_module_graph = false; 458 return false; 459 } 460 } 461 462 void CDSConfig::stop_dumping_full_module_graph(const char* reason) { 463 if (_is_dumping_full_module_graph) { 464 _is_dumping_full_module_graph = false; 465 if (reason != nullptr) { 466 log_info(cds)("full module graph cannot be dumped: %s", reason); 467 } 468 } 469 } 470 471 void CDSConfig::stop_using_full_module_graph(const char* reason) { 472 assert(!ClassLoaderDataShared::is_full_module_graph_loaded(), "you call this function too late!"); 473 if (_is_using_full_module_graph) { 474 _is_using_full_module_graph = false; 475 if (reason != nullptr) { 476 log_info(cds)("full module graph cannot be loaded: %s", reason); 477 } 478 } 479 } 480 #endif // INCLUDE_CDS_JAVA_HEAP