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) && 253 !Arguments::is_module_path_property(key) && 254 !Arguments::is_add_modules_property(key)) { 255 stop_using_optimized_module_handling(); 256 log_info(cds)("optimized module handling: disabled due to incompatible property: %s=%s", key, value); 257 } 258 } 259 260 void CDSConfig::check_incompatible_property(const char* key, const char* value) { 261 static const char* incompatible_properties[] = { 262 "java.system.class.loader", 263 "jdk.module.showModuleResolution", 264 "jdk.module.validation" 265 }; 266 267 for (const char* property : incompatible_properties) { 268 if (strcmp(key, property) == 0) { 269 stop_dumping_full_module_graph(); 270 stop_using_full_module_graph(); 271 log_info(cds)("full module graph: disabled due to incompatible property: %s=%s", key, value); 272 break; 273 } 274 } 275 276 } 277 278 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS. 279 static const char* find_any_unsupported_module_option() { 280 // Note that arguments.cpp has translated the command-line options into properties. If we find an 281 // unsupported property, translate it back to its command-line option for better error reporting. 282 283 // The following properties are checked by Arguments::is_internal_module_property() and cannot be 284 // directly specified in the command-line. 285 static const char* unsupported_module_properties[] = { 286 "jdk.module.limitmods", 287 "jdk.module.upgrade.path" 288 }; 289 static const char* unsupported_module_options[] = { 290 "--limit-modules", 291 "--upgrade-module-path" 292 }; 293 294 assert(ARRAY_SIZE(unsupported_module_properties) == ARRAY_SIZE(unsupported_module_options), "must be"); 295 SystemProperty* sp = Arguments::system_properties(); 296 while (sp != nullptr) { 297 for (uint i = 0; i < ARRAY_SIZE(unsupported_module_properties); i++) { 298 if (strcmp(sp->key(), unsupported_module_properties[i]) == 0) { 299 return unsupported_module_options[i]; 300 } 301 } 302 sp = sp->next(); 303 } 304 305 return nullptr; // not found 306 } 307 308 void CDSConfig::check_unsupported_dumping_module_options() { 309 assert(is_dumping_archive(), "this function is only used with CDS dump time"); 310 const char* option = find_any_unsupported_module_option(); 311 if (option != nullptr) { 312 vm_exit_during_initialization("Cannot use the following option when dumping the shared archive", option); 313 } 314 315 if (module_patching_disables_cds()) { 316 vm_exit_during_initialization( 317 "Cannot use the following option when dumping the shared archive", "--patch-module"); 318 } 319 320 // Check for an exploded module build in use with -Xshare:dump. 321 if (!Arguments::has_jimage()) { 322 vm_exit_during_initialization("Dumping the shared archive is not supported with an exploded module build"); 323 } 324 } 325 326 bool CDSConfig::has_unsupported_runtime_module_options() { 327 assert(is_using_archive(), "this function is only used with -Xshare:{on,auto}"); 328 if (ArchiveClassesAtExit != nullptr) { 329 // dynamic dumping, just return false for now. 330 // check_unsupported_dumping_properties() will be called later to check the same set of 331 // properties, and will exit the VM with the correct error message if the unsupported properties 332 // are used. 333 return false; 334 } 335 const char* option = find_any_unsupported_module_option(); 336 if (option != nullptr) { 337 if (RequireSharedSpaces) { 338 warning("CDS is disabled when the %s option is specified.", option); 339 } else { 340 log_info(cds)("CDS is disabled when the %s option is specified.", option); 341 } 342 return true; 343 } 344 345 if (module_patching_disables_cds()) { 346 if (RequireSharedSpaces) { 347 warning("CDS is disabled when the %s option is specified.", "--patch-module"); 348 } else { 349 log_info(cds)("CDS is disabled when the %s option is specified.", "--patch-module"); 350 } 351 return true; 352 } 353 354 return false; 355 } 356 357 bool CDSConfig::check_vm_args_consistency(bool mode_flag_cmd_line) { 358 if (is_dumping_static_archive()) { 359 if (!mode_flag_cmd_line) { 360 // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive. 361 // 362 // If your classlist is large and you don't care about deterministic dumping, you can use 363 // -Xshare:dump -Xmixed to improve dumping speed. 364 Arguments::set_mode_flags(Arguments::_int); 365 } else if (Arguments::mode() == Arguments::_comp) { 366 // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of 367 // Java code, so there's not much benefit in running -Xcomp. 368 log_info(cds)("reduced -Xcomp to -Xmixed for static dumping"); 369 Arguments::set_mode_flags(Arguments::_mixed); 370 } 371 372 // String deduplication may cause CDS to iterate the strings in different order from one 373 // run to another which resulting in non-determinstic CDS archives. 374 // Disable UseStringDeduplication while dumping CDS archive. 375 UseStringDeduplication = false; 376 } 377 378 // RecordDynamicDumpInfo is not compatible with ArchiveClassesAtExit 379 if (ArchiveClassesAtExit != nullptr && RecordDynamicDumpInfo) { 380 jio_fprintf(defaultStream::output_stream(), 381 "-XX:+RecordDynamicDumpInfo cannot be used with -XX:ArchiveClassesAtExit.\n"); 382 return false; 383 } 384 385 if (ArchiveClassesAtExit == nullptr && !RecordDynamicDumpInfo) { 386 disable_dumping_dynamic_archive(); 387 } else { 388 enable_dumping_dynamic_archive(); 389 } 390 391 if (AutoCreateSharedArchive) { 392 if (SharedArchiveFile == nullptr) { 393 log_warning(cds)("-XX:+AutoCreateSharedArchive requires -XX:SharedArchiveFile"); 394 return false; 395 } 396 if (ArchiveClassesAtExit != nullptr) { 397 log_warning(cds)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit"); 398 return false; 399 } 400 } 401 402 if (is_using_archive() && java_base_module_patching_disables_cds() && module_patching_disables_cds()) { 403 Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched."); 404 } 405 if (is_using_archive() && has_unsupported_runtime_module_options()) { 406 UseSharedSpaces = false; 407 } 408 409 if (is_dumping_archive()) { 410 // Always verify non-system classes during CDS dump 411 if (!BytecodeVerificationRemote) { 412 BytecodeVerificationRemote = true; 413 log_info(cds)("All non-system classes will be verified (-Xverify:remote) during CDS dump time."); 414 } 415 } 416 417 return true; 418 } 419 420 bool CDSConfig::is_using_archive() { 421 return UseSharedSpaces; 422 } 423 424 bool CDSConfig::is_logging_lambda_form_invokers() { 425 return ClassListWriter::is_enabled() || is_dumping_dynamic_archive(); 426 } 427 428 void CDSConfig::stop_using_optimized_module_handling() { 429 _is_using_optimized_module_handling = false; 430 _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling() 431 _is_using_full_module_graph = false; // This requires is_using_optimized_module_handling() 432 } 433 434 #if INCLUDE_CDS_JAVA_HEAP 435 bool CDSConfig::is_dumping_heap() { 436 if (is_valhalla_preview()) { 437 // Not working yet -- e.g., HeapShared::oop_hash() needs to be implemented for value oops 438 return false; 439 } 440 // heap dump is not supported in dynamic dump 441 return is_dumping_static_archive() && HeapShared::can_write(); 442 } 443 444 bool CDSConfig::is_using_full_module_graph() { 445 if (ClassLoaderDataShared::is_full_module_graph_loaded()) { 446 return true; 447 } 448 449 if (!_is_using_full_module_graph) { 450 return false; 451 } 452 453 if (is_using_archive() && ArchiveHeapLoader::can_use()) { 454 // Classes used by the archived full module graph are loaded in JVMTI early phase. 455 assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()), 456 "CDS should be disabled if early class hooks are enabled"); 457 return true; 458 } else { 459 _is_using_full_module_graph = false; 460 return false; 461 } 462 } 463 464 void CDSConfig::stop_dumping_full_module_graph(const char* reason) { 465 if (_is_dumping_full_module_graph) { 466 _is_dumping_full_module_graph = false; 467 if (reason != nullptr) { 468 log_info(cds)("full module graph cannot be dumped: %s", reason); 469 } 470 } 471 } 472 473 void CDSConfig::stop_using_full_module_graph(const char* reason) { 474 assert(!ClassLoaderDataShared::is_full_module_graph_loaded(), "you call this function too late!"); 475 if (_is_using_full_module_graph) { 476 _is_using_full_module_graph = false; 477 if (reason != nullptr) { 478 log_info(cds)("full module graph cannot be loaded: %s", reason); 479 } 480 } 481 } 482 #endif // INCLUDE_CDS_JAVA_HEAP