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