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