1 /* 2 * Copyright (c) 2016, 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/archiveBuilder.hpp" 27 #include "cds/cdsConfig.hpp" 28 #include "cds/metaspaceShared.hpp" 29 #include "classfile/classFileParser.hpp" 30 #include "classfile/classLoader.hpp" 31 #include "classfile/classLoaderData.inline.hpp" 32 #include "classfile/classLoaderDataShared.hpp" 33 #include "classfile/javaAssertions.hpp" 34 #include "classfile/javaClasses.hpp" 35 #include "classfile/javaClasses.inline.hpp" 36 #include "classfile/moduleEntry.hpp" 37 #include "classfile/modules.hpp" 38 #include "classfile/packageEntry.hpp" 39 #include "classfile/stringTable.hpp" 40 #include "classfile/symbolTable.hpp" 41 #include "classfile/systemDictionary.hpp" 42 #include "classfile/vmClasses.hpp" 43 #include "classfile/vmSymbols.hpp" 44 #include "jvm.h" 45 #include "logging/log.hpp" 46 #include "logging/logStream.hpp" 47 #include "memory/resourceArea.hpp" 48 #include "prims/jvmtiExport.hpp" 49 #include "runtime/arguments.hpp" 50 #include "runtime/globals_extension.hpp" 51 #include "runtime/handles.inline.hpp" 52 #include "runtime/javaCalls.hpp" 53 #include "runtime/jniHandles.inline.hpp" 54 #include "utilities/formatBuffer.hpp" 55 #include "utilities/stringUtils.hpp" 56 #include "utilities/utf8.hpp" 57 58 static bool verify_module_name(const char *module_name, int len) { 59 assert(module_name != nullptr, "invariant"); 60 return (len > 0 && len <= Symbol::max_length()); 61 } 62 63 static bool verify_package_name(const char* package_name, int len) { 64 assert(package_name != nullptr, "Package name derived from non-null jstring can't be null"); 65 return (len > 0 && len <= Symbol::max_length() && 66 ClassFileParser::verify_unqualified_name(package_name, len, 67 ClassFileParser::LegalClass)); 68 } 69 70 static char* get_module_name(oop module, int& len, TRAPS) { 71 oop name_oop = java_lang_Module::name(module); 72 if (name_oop == nullptr) { 73 THROW_MSG_NULL(vmSymbols::java_lang_NullPointerException(), "Null module name"); 74 } 75 char* module_name = java_lang_String::as_utf8_string(name_oop, len); 76 if (!verify_module_name(module_name, len)) { 77 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), 78 err_msg("Invalid module name: %s", module_name)); 79 } 80 return module_name; 81 } 82 83 static Symbol* as_symbol(jstring str_object) { 84 if (str_object == nullptr) { 85 return nullptr; 86 } 87 int len; 88 char* str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(str_object), len); 89 return SymbolTable::new_symbol(str, len); 90 } 91 92 ModuleEntryTable* Modules::get_module_entry_table(Handle h_loader) { 93 // This code can be called during start-up, before the classLoader's classLoader data got 94 // created. So, call register_loader() to make sure the classLoader data gets created. 95 ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader); 96 return loader_cld->modules(); 97 } 98 99 static PackageEntryTable* get_package_entry_table(Handle h_loader) { 100 // This code can be called during start-up, before the classLoader's classLoader data got 101 // created. So, call register_loader() to make sure the classLoader data gets created. 102 ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader); 103 return loader_cld->packages(); 104 } 105 106 static ModuleEntry* get_module_entry(Handle module, TRAPS) { 107 if (!java_lang_Module::is_instance(module())) { 108 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), 109 "module is not an instance of type java.lang.Module"); 110 } 111 return java_lang_Module::module_entry(module()); 112 } 113 114 115 static PackageEntry* get_locked_package_entry(ModuleEntry* module_entry, const char* package_name, int len) { 116 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 117 assert(package_name != nullptr, "Precondition"); 118 TempNewSymbol pkg_symbol = SymbolTable::new_symbol(package_name, len); 119 PackageEntryTable* package_entry_table = module_entry->loader_data()->packages(); 120 assert(package_entry_table != nullptr, "Unexpected null package entry table"); 121 PackageEntry* package_entry = package_entry_table->locked_lookup_only(pkg_symbol); 122 assert(package_entry == nullptr || package_entry->module() == module_entry, "Unexpectedly found a package linked to another module"); 123 return package_entry; 124 } 125 126 static PackageEntry* get_package_entry_by_name(Symbol* package, Handle h_loader) { 127 if (package != nullptr) { 128 PackageEntryTable* const package_entry_table = 129 get_package_entry_table(h_loader); 130 assert(package_entry_table != nullptr, "Unexpected null package entry table"); 131 return package_entry_table->lookup_only(package); 132 } 133 return nullptr; 134 } 135 136 bool Modules::is_package_defined(Symbol* package, Handle h_loader) { 137 PackageEntry* res = get_package_entry_by_name(package, h_loader); 138 return res != nullptr; 139 } 140 141 // Converts the String oop to an internal package 142 // Will use the provided buffer if it's sufficiently large, otherwise allocates 143 // a resource array 144 // The length of the resulting string will be assigned to utf8_len 145 static const char* as_internal_package(oop package_string, char* buf, int buflen, int& utf8_len) { 146 char* package_name = java_lang_String::as_utf8_string_full(package_string, buf, buflen, utf8_len); 147 148 // Turn all '/'s into '.'s 149 for (int index = 0; index < utf8_len; index++) { 150 if (package_name[index] == JVM_SIGNATURE_DOT) { 151 package_name[index] = JVM_SIGNATURE_SLASH; 152 } 153 } 154 return package_name; 155 } 156 157 static void define_javabase_module(Handle module_handle, jstring version, jstring location, 158 objArrayHandle pkgs, int num_packages, TRAPS) { 159 ResourceMark rm(THREAD); 160 161 // Obtain java.base's module version 162 TempNewSymbol version_symbol = as_symbol(version); 163 164 // Obtain java.base's location 165 TempNewSymbol location_symbol = as_symbol(location); 166 167 // Check that the packages are syntactically ok. 168 char buf[128]; 169 GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages); 170 for (int x = 0; x < num_packages; x++) { 171 oop pkg_str = pkgs->obj_at(x); 172 173 if (pkg_str == nullptr || pkg_str->klass() != vmClasses::String_klass()) { 174 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 175 err_msg("Bad package name")); 176 } 177 178 int package_len; 179 const char* package_name = as_internal_package(pkg_str, buf, sizeof(buf), package_len); 180 if (!verify_package_name(package_name, package_len)) { 181 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 182 err_msg("Invalid package name: %s for module: " JAVA_BASE_NAME, package_name)); 183 } 184 Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, package_len); 185 pkg_list->append(pkg_symbol); 186 } 187 188 // Validate java_base's loader is the boot loader. 189 oop loader = java_lang_Module::loader(module_handle()); 190 if (loader != nullptr) { 191 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 192 "Class loader must be the boot class loader"); 193 } 194 Handle h_loader(THREAD, loader); 195 196 // Ensure the boot loader's PackageEntryTable has been created 197 PackageEntryTable* package_table = get_package_entry_table(h_loader); 198 assert(pkg_list->length() == 0 || package_table != nullptr, "Bad package_table"); 199 200 // Ensure java.base's ModuleEntry has been created 201 assert(ModuleEntryTable::javabase_moduleEntry() != nullptr, "No ModuleEntry for " JAVA_BASE_NAME); 202 203 bool duplicate_javabase = false; 204 { 205 MutexLocker m1(THREAD, Module_lock); 206 207 if (ModuleEntryTable::javabase_defined()) { 208 duplicate_javabase = true; 209 } else { 210 211 // Verify that all java.base packages created during bootstrapping are in 212 // pkg_list. If any are not in pkg_list, than a non-java.base class was 213 // loaded erroneously pre java.base module definition. 214 package_table->verify_javabase_packages(pkg_list); 215 216 // loop through and add any new packages for java.base 217 for (int x = 0; x < pkg_list->length(); x++) { 218 // Some of java.base's packages were added early in bootstrapping, ignore duplicates. 219 package_table->locked_create_entry_if_absent(pkg_list->at(x), 220 ModuleEntryTable::javabase_moduleEntry()); 221 assert(package_table->locked_lookup_only(pkg_list->at(x)) != nullptr, 222 "Unable to create a " JAVA_BASE_NAME " package entry"); 223 // Unable to have a GrowableArray of TempNewSymbol. Must decrement the refcount of 224 // the Symbol* that was created above for each package. The refcount was incremented 225 // by SymbolTable::new_symbol and as well by the PackageEntry creation. 226 pkg_list->at(x)->decrement_refcount(); 227 } 228 229 // Finish defining java.base's ModuleEntry 230 ModuleEntryTable::finalize_javabase(module_handle, version_symbol, location_symbol); 231 } 232 } 233 if (duplicate_javabase) { 234 THROW_MSG(vmSymbols::java_lang_InternalError(), 235 "Module " JAVA_BASE_NAME " is already defined"); 236 } 237 238 // Only the thread that actually defined the base module will get here, 239 // so no locking is needed. 240 241 // Patch any previously loaded class's module field with java.base's java.lang.Module. 242 ModuleEntryTable::patch_javabase_entries(THREAD, module_handle); 243 244 log_info(module, load)(JAVA_BASE_NAME " location: %s", 245 location_symbol != nullptr ? location_symbol->as_C_string() : "nullptr"); 246 log_debug(module)("define_javabase_module(): Definition of module: " 247 JAVA_BASE_NAME ", version: %s, location: %s, package #: %d", 248 version_symbol != nullptr ? version_symbol->as_C_string() : "nullptr", 249 location_symbol != nullptr ? location_symbol->as_C_string() : "nullptr", 250 pkg_list->length()); 251 252 // packages defined to java.base 253 if (log_is_enabled(Trace, module)) { 254 for (int x = 0; x < pkg_list->length(); x++) { 255 log_trace(module)("define_javabase_module(): creation of package %s for module " JAVA_BASE_NAME, 256 (pkg_list->at(x))->as_C_string()); 257 } 258 } 259 } 260 261 // Caller needs ResourceMark. 262 static void throw_dup_pkg_exception(const char* module_name, PackageEntry* package, TRAPS) { 263 const char* package_name = package->name()->as_C_string(); 264 if (package->module()->is_named()) { 265 THROW_MSG(vmSymbols::java_lang_IllegalStateException(), 266 err_msg("Package %s for module %s is already in another module, %s, defined to the class loader", 267 package_name, module_name, package->module()->name()->as_C_string())); 268 } else { 269 THROW_MSG(vmSymbols::java_lang_IllegalStateException(), 270 err_msg("Package %s for module %s is already in the unnamed module defined to the class loader", 271 package_name, module_name)); 272 } 273 } 274 275 void Modules::define_module(Handle module, jboolean is_open, jstring version, 276 jstring location, jobjectArray packages, TRAPS) { 277 ResourceMark rm(THREAD); 278 279 if (module.is_null()) { 280 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object"); 281 } 282 283 if (!java_lang_Module::is_instance(module())) { 284 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 285 "module is not an instance of type java.lang.Module"); 286 } 287 288 int module_name_len; 289 char* module_name = get_module_name(module(), module_name_len, CHECK); 290 if (module_name == nullptr) { 291 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 292 "Module name cannot be null"); 293 } 294 295 // Resolve packages 296 objArrayHandle packages_h(THREAD, objArrayOop(JNIHandles::resolve(packages))); 297 int num_packages = (packages_h.is_null() ? 0 : packages_h->length()); 298 if (strncmp(module_name, "jdk.proxy", 9) != 0) { 299 check_cds_restrictions(Handle(), Handle(), CHECK); 300 } 301 302 // Special handling of java.base definition 303 if (strcmp(module_name, JAVA_BASE_NAME) == 0) { 304 assert(is_open == JNI_FALSE, "java.base module cannot be open"); 305 define_javabase_module(module, version, location, packages_h, num_packages, CHECK); 306 return; 307 } 308 309 oop loader = java_lang_Module::loader(module()); 310 // Make sure loader is not the jdk.internal.reflect.DelegatingClassLoader. 311 if (loader != java_lang_ClassLoader::non_reflection_class_loader(loader)) { 312 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 313 "Class loader is an invalid delegating class loader"); 314 } 315 Handle h_loader = Handle(THREAD, loader); 316 // define_module can be called during start-up, before the class loader's ClassLoaderData 317 // has been created. SystemDictionary::register_loader ensures creation, if needed. 318 ClassLoaderData* loader_data = SystemDictionary::register_loader(h_loader); 319 assert(loader_data != nullptr, "class loader data shouldn't be null"); 320 321 // Only modules defined to either the boot or platform class loader, can define a "java/" package. 322 bool java_pkg_disallowed = !h_loader.is_null() && 323 !SystemDictionary::is_platform_class_loader(h_loader()); 324 325 // Check that the list of packages has no duplicates and that the 326 // packages are syntactically ok. 327 char buf[128]; 328 GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages); 329 for (int x = 0; x < num_packages; x++) { 330 oop pkg_str = packages_h->obj_at(x); 331 if (pkg_str == nullptr || pkg_str->klass() != vmClasses::String_klass()) { 332 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 333 err_msg("Bad package name")); 334 } 335 336 int package_len; 337 const char* package_name = as_internal_package(pkg_str, buf, sizeof(buf), package_len); 338 if (!verify_package_name(package_name, package_len)) { 339 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 340 err_msg("Invalid package name: %s for module: %s", 341 package_name, module_name)); 342 } 343 344 // Only modules defined to either the boot or platform class loader, can define a "java/" package. 345 if (java_pkg_disallowed && 346 (strncmp(package_name, JAVAPKG, JAVAPKG_LEN) == 0 && 347 (package_name[JAVAPKG_LEN] == JVM_SIGNATURE_SLASH || package_name[JAVAPKG_LEN] == '\0'))) { 348 const char* class_loader_name = loader_data->loader_name_and_id(); 349 size_t pkg_len = strlen(package_name); 350 char* pkg_name = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, pkg_len + 1); 351 strncpy(pkg_name, package_name, pkg_len + 1); 352 StringUtils::replace_no_expand(pkg_name, "/", "."); 353 const char* msg_text1 = "Class loader (instance of): "; 354 const char* msg_text2 = " tried to define prohibited package name: "; 355 size_t len = strlen(msg_text1) + strlen(class_loader_name) + strlen(msg_text2) + pkg_len + 1; 356 char* message = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len); 357 jio_snprintf(message, len, "%s%s%s%s", msg_text1, class_loader_name, msg_text2, pkg_name); 358 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), message); 359 } 360 361 Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, package_len); 362 pkg_list->append(pkg_symbol); 363 } 364 365 ModuleEntryTable* module_table = get_module_entry_table(h_loader); 366 assert(module_table != nullptr, "module entry table shouldn't be null"); 367 368 // Create symbol* entry for module name. 369 TempNewSymbol module_symbol = SymbolTable::new_symbol(module_name, module_name_len); 370 371 bool dupl_modules = false; 372 373 // Create symbol for module version. 374 TempNewSymbol version_symbol = as_symbol(version); 375 376 // Create symbol* entry for module location. 377 TempNewSymbol location_symbol = as_symbol(location); 378 379 PackageEntryTable* package_table = nullptr; 380 PackageEntry* existing_pkg = nullptr; 381 { 382 MutexLocker ml(THREAD, Module_lock); 383 384 if (num_packages > 0) { 385 package_table = get_package_entry_table(h_loader); 386 assert(package_table != nullptr, "Missing package_table"); 387 388 // Check that none of the packages exist in the class loader's package table. 389 for (int x = 0; x < pkg_list->length(); x++) { 390 existing_pkg = package_table->locked_lookup_only(pkg_list->at(x)); 391 if (existing_pkg != nullptr) { 392 // This could be because the module was already defined. If so, 393 // report that error instead of the package error. 394 if (module_table->lookup_only(module_symbol) != nullptr) { 395 dupl_modules = true; 396 } 397 break; 398 } 399 } 400 } // if (num_packages > 0)... 401 402 // Add the module and its packages. 403 if (!dupl_modules && existing_pkg == nullptr) { 404 if (module_table->lookup_only(module_symbol) == nullptr) { 405 // Create the entry for this module in the class loader's module entry table. 406 ModuleEntry* module_entry = module_table->locked_create_entry(module, 407 (is_open == JNI_TRUE), module_symbol, 408 version_symbol, location_symbol, loader_data); 409 assert(module_entry != nullptr, "module_entry creation failed"); 410 411 // Add the packages. 412 assert(pkg_list->length() == 0 || package_table != nullptr, "Bad package table"); 413 for (int y = 0; y < pkg_list->length(); y++) { 414 package_table->locked_create_entry(pkg_list->at(y), module_entry); 415 416 // Unable to have a GrowableArray of TempNewSymbol. Must decrement the refcount of 417 // the Symbol* that was created above for each package. The refcount was incremented 418 // by SymbolTable::new_symbol and as well by the PackageEntry creation. 419 pkg_list->at(y)->decrement_refcount(); 420 } 421 422 // Store pointer to ModuleEntry record in java.lang.Module object. 423 java_lang_Module::set_module_entry(module(), module_entry); 424 } else { 425 dupl_modules = true; 426 } 427 } 428 } // Release the lock 429 430 // any errors ? 431 if (dupl_modules) { 432 THROW_MSG(vmSymbols::java_lang_IllegalStateException(), 433 err_msg("Module %s is already defined", module_name)); 434 } else if (existing_pkg != nullptr) { 435 throw_dup_pkg_exception(module_name, existing_pkg, CHECK); 436 } 437 438 log_info(module, load)("%s location: %s", module_name, 439 location_symbol != nullptr ? location_symbol->as_C_string() : "null"); 440 LogTarget(Debug, module) lt; 441 if (lt.is_enabled()) { 442 LogStream ls(lt); 443 ls.print("define_module(): creation of module: %s, version: %s, location: %s, ", 444 module_name, version_symbol != nullptr ? version_symbol->as_C_string() : "null", 445 location_symbol != nullptr ? location_symbol->as_C_string() : "null"); 446 loader_data->print_value_on(&ls); 447 ls.print_cr(", package #: %d", pkg_list->length()); 448 for (int y = 0; y < pkg_list->length(); y++) { 449 log_trace(module)("define_module(): creation of package %s for module %s", 450 (pkg_list->at(y))->as_C_string(), module_name); 451 } 452 } 453 454 // If the module is defined to the boot loader and an exploded build is being 455 // used, prepend <java.home>/modules/modules_name to the boot class path. 456 if (h_loader.is_null() && !ClassLoader::has_jrt_entry()) { 457 ClassLoader::add_to_exploded_build_list(THREAD, module_symbol); 458 } 459 460 #if COMPILER2_OR_JVMCI 461 // Special handling of jdk.incubator.vector 462 if (strcmp(module_name, "jdk.incubator.vector") == 0) { 463 if (FLAG_IS_DEFAULT(EnableVectorSupport)) { 464 FLAG_SET_DEFAULT(EnableVectorSupport, true); 465 } 466 if (EnableVectorSupport && FLAG_IS_DEFAULT(EnableVectorReboxing)) { 467 FLAG_SET_DEFAULT(EnableVectorReboxing, true); 468 } 469 if (EnableVectorSupport && EnableVectorReboxing && FLAG_IS_DEFAULT(EnableVectorAggressiveReboxing)) { 470 FLAG_SET_DEFAULT(EnableVectorAggressiveReboxing, true); 471 } 472 if (EnableVectorSupport && FLAG_IS_DEFAULT(UseVectorStubs)) { 473 FLAG_SET_DEFAULT(UseVectorStubs, true); 474 } 475 log_info(compilation)("EnableVectorSupport=%s", (EnableVectorSupport ? "true" : "false")); 476 log_info(compilation)("EnableVectorReboxing=%s", (EnableVectorReboxing ? "true" : "false")); 477 log_info(compilation)("EnableVectorAggressiveReboxing=%s", (EnableVectorAggressiveReboxing ? "true" : "false")); 478 log_info(compilation)("UseVectorStubs=%s", (UseVectorStubs ? "true" : "false")); 479 } 480 #endif // COMPILER2_OR_JVMCI 481 } 482 483 #if INCLUDE_CDS_JAVA_HEAP 484 static bool _seen_platform_unnamed_module = false; 485 static bool _seen_system_unnamed_module = false; 486 487 // Validate the states of an java.lang.Module oop to be archived. 488 // 489 // Returns true iff the oop has an archived ModuleEntry. 490 bool Modules::check_archived_module_oop(oop orig_module_obj) { 491 assert(CDSConfig::is_dumping_full_module_graph(), "must be"); 492 assert(java_lang_Module::is_instance(orig_module_obj), "must be"); 493 494 ModuleEntry* orig_module_ent = java_lang_Module::module_entry_raw(orig_module_obj); 495 if (orig_module_ent == nullptr) { 496 // These special java.lang.Module oops are created in Java code. They are not 497 // defined via Modules::define_module(), so they don't have a ModuleEntry: 498 // java.lang.Module::ALL_UNNAMED_MODULE 499 // java.lang.Module::EVERYONE_MODULE 500 // jdk.internal.loader.ClassLoaders$BootClassLoader::unnamedModule 501 log_info(cds, module)("Archived java.lang.Module oop " PTR_FORMAT " with no ModuleEntry*", p2i(orig_module_obj)); 502 assert(java_lang_Module::name(orig_module_obj) == nullptr, "must be unnamed"); 503 return false; 504 } else { 505 // This java.lang.Module oop has an ModuleEntry*. Check if the latter is archived. 506 if (log_is_enabled(Info, cds, module)) { 507 ResourceMark rm; 508 LogStream ls(Log(cds, module)::info()); 509 ls.print("Archived java.lang.Module oop " PTR_FORMAT " for ", p2i(orig_module_obj)); 510 orig_module_ent->print(&ls); 511 } 512 513 // We only archive the default module graph, which should contain only java.lang.Module oops 514 // for the 3 built-in loaders (boot/platform/system) 515 ClassLoaderData* loader_data = orig_module_ent->loader_data(); 516 assert(loader_data->is_builtin_class_loader_data(), "must be"); 517 518 if (orig_module_ent->name() != nullptr) { 519 // For each named module, we archive both the java.lang.Module oop and the ModuleEntry. 520 assert(orig_module_ent->has_been_archived(), "sanity"); 521 return true; 522 } else { 523 // We only archive two unnamed module oops (for platform and system loaders). These do NOT have an archived 524 // ModuleEntry. 525 // 526 // At runtime, these oops are fetched from java_lang_ClassLoader::unnamedModule(loader) and 527 // are initialized in ClassLoaderData::ClassLoaderData() => ModuleEntry::create_unnamed_module(), where 528 // a new ModuleEntry is allocated. 529 assert(!loader_data->is_boot_class_loader_data(), "unnamed module for boot loader should be not archived"); 530 assert(!orig_module_ent->has_been_archived(), "sanity"); 531 532 if (SystemDictionary::is_platform_class_loader(loader_data->class_loader())) { 533 assert(!_seen_platform_unnamed_module, "only once"); 534 _seen_platform_unnamed_module = true; 535 } else if (SystemDictionary::is_system_class_loader(loader_data->class_loader())) { 536 assert(!_seen_system_unnamed_module, "only once"); 537 _seen_system_unnamed_module = true; 538 } else { 539 // The java.lang.Module oop and ModuleEntry of the unnamed module of the boot loader are 540 // not in the archived module graph. These are always allocated at runtime. 541 ShouldNotReachHere(); 542 } 543 return false; 544 } 545 } 546 } 547 548 void Modules::update_oops_in_archived_module(oop orig_module_obj, int archived_module_root_index) { 549 // This java.lang.Module oop must have an archived ModuleEntry 550 assert(check_archived_module_oop(orig_module_obj) == true, "sanity"); 551 552 // We remember the oop inside the ModuleEntry::_archived_module_index. At runtime, we use 553 // this index to reinitialize the ModuleEntry inside ModuleEntry::restore_archived_oops(). 554 // 555 // ModuleEntry::verify_archived_module_entries(), called below, ensures that every archived 556 // ModuleEntry has been assigned an _archived_module_index. 557 ModuleEntry* orig_module_ent = java_lang_Module::module_entry_raw(orig_module_obj); 558 ModuleEntry::get_archived_entry(orig_module_ent)->update_oops_in_archived_module(archived_module_root_index); 559 } 560 561 void Modules::verify_archived_modules() { 562 ModuleEntry::verify_archived_module_entries(); 563 } 564 565 char* Modules::_archived_main_module_name = nullptr; 566 567 void Modules::dump_main_module_name() { 568 const char* module_name = Arguments::get_property("jdk.module.main"); 569 if (module_name != nullptr) { 570 _archived_main_module_name = ArchiveBuilder::current()->ro_strdup(module_name); 571 } 572 ArchivePtrMarker::mark_pointer(&_archived_main_module_name); 573 } 574 575 void Modules::serialize(SerializeClosure* soc) { 576 soc->do_ptr(&_archived_main_module_name); 577 if (soc->reading()) { 578 const char* runtime_main_module = Arguments::get_property("jdk.module.main"); 579 log_info(cds)("_archived_main_module_name %s", 580 _archived_main_module_name != nullptr ? _archived_main_module_name : "(null)"); 581 bool disable = false; 582 if (runtime_main_module == nullptr) { 583 if (_archived_main_module_name != nullptr) { 584 log_info(cds)("Module %s specified during dump time but not during runtime", _archived_main_module_name); 585 disable = true; 586 } 587 } else { 588 if (_archived_main_module_name == nullptr) { 589 log_info(cds)("Module %s specified during runtime but not during dump time", runtime_main_module); 590 disable = true; 591 } else if (strcmp(runtime_main_module, _archived_main_module_name) != 0) { 592 log_info(cds)("Mismatched modules: runtime %s dump time %s", runtime_main_module, _archived_main_module_name); 593 disable = true; 594 } 595 } 596 597 if (disable) { 598 log_info(cds)("Disabling optimized module handling"); 599 CDSConfig::stop_using_optimized_module_handling(); 600 } 601 log_info(cds)("optimized module handling: %s", CDSConfig::is_using_optimized_module_handling() ? "enabled" : "disabled"); 602 log_info(cds)("full module graph: %s", CDSConfig::is_using_full_module_graph() ? "enabled" : "disabled"); 603 } 604 } 605 606 void Modules::define_archived_modules(Handle h_platform_loader, Handle h_system_loader, TRAPS) { 607 assert(CDSConfig::is_using_full_module_graph(), "must be"); 608 609 // We don't want the classes used by the archived full module graph to be redefined by JVMTI. 610 // Luckily, such classes are loaded in the JVMTI "early" phase, and CDS is disabled if a JVMTI 611 // agent wants to redefine classes in this phase. 612 JVMTI_ONLY(assert(JvmtiExport::is_early_phase(), "must be")); 613 assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()), 614 "CDS should be disabled if early class hooks are enabled"); 615 616 Handle java_base_module(THREAD, ClassLoaderDataShared::restore_archived_oops_for_null_class_loader_data()); 617 // Patch any previously loaded class's module field with java.base's java.lang.Module. 618 ModuleEntryTable::patch_javabase_entries(THREAD, java_base_module); 619 620 if (h_platform_loader.is_null()) { 621 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null platform loader object"); 622 } 623 624 if (h_system_loader.is_null()) { 625 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null system loader object"); 626 } 627 628 ClassLoaderData* platform_loader_data = SystemDictionary::register_loader(h_platform_loader); 629 SystemDictionary::set_platform_loader(platform_loader_data); 630 ClassLoaderDataShared::restore_java_platform_loader_from_archive(platform_loader_data); 631 632 ClassLoaderData* system_loader_data = SystemDictionary::register_loader(h_system_loader); 633 SystemDictionary::set_system_loader(system_loader_data); 634 // system_loader_data here is always an instance of jdk.internal.loader.ClassLoader$AppClassLoader. 635 // However, if -Djava.system.class.loader=xxx is specified, java_platform_loader() would 636 // be an instance of a user-defined class, so make sure this never happens. 637 assert(Arguments::get_property("java.system.class.loader") == nullptr, 638 "archived full module should have been disabled if -Djava.system.class.loader is specified"); 639 ClassLoaderDataShared::restore_java_system_loader_from_archive(system_loader_data); 640 } 641 642 void Modules::check_cds_restrictions(Handle module1, Handle module2, TRAPS) { 643 if (CDSConfig::is_dumping_full_module_graph() && Universe::is_module_initialized()) { 644 if (ArchiveDynamicProxies && (is_dynamic_proxy_module(module1) || is_dynamic_proxy_module(module2))) { 645 // The only the we allow is to add or modify the jdk.proxy?? modules that are used for dynamic proxies. 646 } else { 647 THROW_MSG(vmSymbols::java_lang_UnsupportedOperationException(), 648 "During -Xshare:dump, module system cannot be modified after it's initialized"); 649 } 650 } 651 } 652 653 #endif // INCLUDE_CDS_JAVA_HEAP 654 655 bool Modules::is_dynamic_proxy_module(Handle module) { 656 if (!module.is_null()) { 657 ModuleEntry* module_entry = java_lang_Module::module_entry(module()); 658 return is_dynamic_proxy_module(module_entry); 659 } 660 return false; 661 } 662 663 bool Modules::is_dynamic_proxy_module(ModuleEntry* module_entry) { 664 return (module_entry != nullptr && module_entry->is_named() && module_entry->name()->starts_with("jdk.proxy")); 665 } 666 667 void Modules::set_bootloader_unnamed_module(Handle module, TRAPS) { 668 ResourceMark rm(THREAD); 669 670 if (module.is_null()) { 671 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object"); 672 } 673 if (!java_lang_Module::is_instance(module())) { 674 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 675 "module is not an instance of type java.lang.Module"); 676 } 677 678 // Ensure that this is an unnamed module 679 oop name = java_lang_Module::name(module()); 680 if (name != nullptr) { 681 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 682 "boot loader's unnamed module's java.lang.Module has a name"); 683 } 684 685 // Validate java_base's loader is the boot loader. 686 oop loader = java_lang_Module::loader(module()); 687 if (loader != nullptr) { 688 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 689 "Class loader must be the boot class loader"); 690 } 691 692 log_debug(module)("set_bootloader_unnamed_module(): recording unnamed module for boot loader"); 693 694 // Set java.lang.Module for the boot loader's unnamed module 695 ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data(); 696 ModuleEntry* unnamed_module = boot_loader_data->unnamed_module(); 697 assert(unnamed_module != nullptr, "boot loader's unnamed ModuleEntry not defined"); 698 unnamed_module->set_module(boot_loader_data->add_handle(module)); 699 // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module object. 700 java_lang_Module::set_module_entry(module(), unnamed_module); 701 } 702 703 void Modules::add_module_exports(Handle from_module, jstring package_name, Handle to_module, TRAPS) { 704 check_cds_restrictions(from_module, to_module, CHECK); 705 706 if (package_name == nullptr) { 707 THROW_MSG(vmSymbols::java_lang_NullPointerException(), 708 "package is null"); 709 } 710 if (from_module.is_null()) { 711 THROW_MSG(vmSymbols::java_lang_NullPointerException(), 712 "from_module is null"); 713 } 714 ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK); 715 if (from_module_entry == nullptr) { 716 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 717 "from_module cannot be found"); 718 } 719 720 // All packages in unnamed and open modules are exported by default. 721 if (!from_module_entry->is_named() || from_module_entry->is_open()) return; 722 723 ModuleEntry* to_module_entry; 724 if (to_module.is_null()) { 725 to_module_entry = nullptr; // It's an unqualified export. 726 } else { 727 to_module_entry = get_module_entry(to_module, CHECK); 728 if (to_module_entry == nullptr) { 729 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 730 "to_module is invalid"); 731 } 732 } 733 734 PackageEntry* package_entry = nullptr; 735 char buf[128]; 736 int package_len; 737 738 ResourceMark rm(THREAD); 739 const char* pkg = as_internal_package(JNIHandles::resolve_non_null(package_name), buf, sizeof(buf), package_len); 740 { 741 MutexLocker ml(THREAD, Module_lock); 742 package_entry = get_locked_package_entry(from_module_entry, pkg, package_len); 743 // Do nothing if modules are the same 744 // If the package is not found we'll throw an exception later 745 if (from_module_entry != to_module_entry && 746 package_entry != nullptr) { 747 package_entry->set_exported(to_module_entry); 748 } 749 } 750 751 // Handle errors and logging outside locked section 752 if (package_entry == nullptr) { 753 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 754 err_msg("Package %s not found in from_module %s", 755 pkg != nullptr ? pkg : "", 756 from_module_entry->name()->as_C_string())); 757 } 758 759 if (log_is_enabled(Debug, module)) { 760 log_debug(module)("add_module_exports(): package %s in module %s is exported to module %s", 761 package_entry->name()->as_C_string(), 762 from_module_entry->name()->as_C_string(), 763 to_module_entry == nullptr ? "null" : 764 to_module_entry->is_named() ? 765 to_module_entry->name()->as_C_string() : UNNAMED_MODULE); 766 } 767 } 768 769 770 void Modules::add_module_exports_qualified(Handle from_module, jstring package, 771 Handle to_module, TRAPS) { 772 check_cds_restrictions(from_module, to_module, CHECK); 773 if (to_module.is_null()) { 774 THROW_MSG(vmSymbols::java_lang_NullPointerException(), 775 "to_module is null"); 776 } 777 add_module_exports(from_module, package, to_module, CHECK); 778 } 779 780 void Modules::add_reads_module(Handle from_module, Handle to_module, TRAPS) { 781 check_cds_restrictions(from_module, to_module, CHECK); 782 if (from_module.is_null()) { 783 THROW_MSG(vmSymbols::java_lang_NullPointerException(), 784 "from_module is null"); 785 } 786 787 ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK); 788 if (from_module_entry == nullptr) { 789 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 790 "from_module is not valid"); 791 } 792 793 ModuleEntry* to_module_entry; 794 if (!to_module.is_null()) { 795 to_module_entry = get_module_entry(to_module, CHECK); 796 if (to_module_entry == nullptr) { 797 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 798 "to_module is invalid"); 799 } 800 } else { 801 to_module_entry = nullptr; 802 } 803 804 ResourceMark rm(THREAD); 805 log_debug(module)("add_reads_module(): Adding read from module %s to module %s", 806 from_module_entry->is_named() ? 807 from_module_entry->name()->as_C_string() : UNNAMED_MODULE, 808 to_module_entry == nullptr ? "all unnamed" : 809 (to_module_entry->is_named() ? 810 to_module_entry->name()->as_C_string() : UNNAMED_MODULE)); 811 812 // if modules are the same or if from_module is unnamed then no need to add the read. 813 if (from_module_entry != to_module_entry && from_module_entry->is_named()) { 814 from_module_entry->add_read(to_module_entry); 815 } 816 } 817 818 // This method is called by JFR and JNI. 819 jobject Modules::get_module(jclass clazz, TRAPS) { 820 assert(ModuleEntryTable::javabase_defined(), 821 "Attempt to call get_module before " JAVA_BASE_NAME " is defined"); 822 823 if (clazz == nullptr) { 824 THROW_MSG_(vmSymbols::java_lang_NullPointerException(), 825 "class is null", nullptr); 826 } 827 oop mirror = JNIHandles::resolve_non_null(clazz); 828 if (mirror == nullptr) { 829 log_debug(module)("get_module(): no mirror, returning nullptr"); 830 return nullptr; 831 } 832 if (!java_lang_Class::is_instance(mirror)) { 833 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), 834 "Invalid class", nullptr); 835 } 836 837 oop module = java_lang_Class::module(mirror); 838 839 assert(module != nullptr, "java.lang.Class module field not set"); 840 assert(java_lang_Module::is_instance(module), "module is not an instance of type java.lang.Module"); 841 842 LogTarget(Debug,module) lt; 843 if (lt.is_enabled()) { 844 ResourceMark rm(THREAD); 845 LogStream ls(lt); 846 Klass* klass = java_lang_Class::as_Klass(mirror); 847 oop module_name = java_lang_Module::name(module); 848 if (module_name != nullptr) { 849 ls.print("get_module(): module "); 850 java_lang_String::print(module_name, tty); 851 } else { 852 ls.print("get_module(): Unnamed Module"); 853 } 854 if (klass != nullptr) { 855 ls.print_cr(" for class %s", klass->external_name()); 856 } else { 857 ls.print_cr(" for primitive class"); 858 } 859 } 860 861 return JNIHandles::make_local(THREAD, module); 862 } 863 864 oop Modules::get_named_module(Handle h_loader, const char* package_name) { 865 assert(ModuleEntryTable::javabase_defined(), 866 "Attempt to call get_named_module before " JAVA_BASE_NAME " is defined"); 867 assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()), 868 "Class loader is not a subclass of java.lang.ClassLoader"); 869 assert(package_name != nullptr, "the package_name should not be null"); 870 871 if (strlen(package_name) == 0) { 872 return nullptr; 873 } 874 TempNewSymbol package_sym = SymbolTable::new_symbol(package_name); 875 const PackageEntry* const pkg_entry = 876 get_package_entry_by_name(package_sym, h_loader); 877 const ModuleEntry* const module_entry = (pkg_entry != nullptr ? pkg_entry->module() : nullptr); 878 879 if (module_entry != nullptr && module_entry->module() != nullptr && module_entry->is_named()) { 880 return module_entry->module(); 881 } 882 return nullptr; 883 } 884 885 // Export package in module to all unnamed modules. 886 void Modules::add_module_exports_to_all_unnamed(Handle module, jstring package_name, TRAPS) { 887 check_cds_restrictions(Handle(), module, CHECK); 888 if (module.is_null()) { 889 THROW_MSG(vmSymbols::java_lang_NullPointerException(), 890 "module is null"); 891 } 892 if (package_name == nullptr) { 893 THROW_MSG(vmSymbols::java_lang_NullPointerException(), 894 "package is null"); 895 } 896 ModuleEntry* module_entry = get_module_entry(module, CHECK); 897 if (module_entry == nullptr) { 898 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 899 "module is invalid"); 900 } 901 902 // No-op for unnamed module and open modules 903 if (!module_entry->is_named() || module_entry->is_open()) 904 return; 905 906 ResourceMark rm(THREAD); 907 char buf[128]; 908 int pkg_len; 909 const char* pkg = as_internal_package(JNIHandles::resolve_non_null(package_name), buf, sizeof(buf), pkg_len); 910 PackageEntry* package_entry = nullptr; 911 { 912 MutexLocker m1(THREAD, Module_lock); 913 package_entry = get_locked_package_entry(module_entry, pkg, pkg_len); 914 915 // Mark package as exported to all unnamed modules. 916 if (package_entry != nullptr) { 917 package_entry->set_is_exported_allUnnamed(); 918 } 919 } 920 921 // Handle errors and logging outside locked section 922 if (package_entry == nullptr) { 923 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 924 err_msg("Package %s not found in module %s", 925 pkg != nullptr ? pkg : "", 926 module_entry->name()->as_C_string())); 927 } 928 929 if (log_is_enabled(Debug, module)) { 930 log_debug(module)("add_module_exports_to_all_unnamed(): package %s in module" 931 " %s is exported to all unnamed modules", 932 package_entry->name()->as_C_string(), 933 module_entry->name()->as_C_string()); 934 } 935 }