1 /* 2 * Copyright (c) 2016, 2020, 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 "jvm.h" 27 #include "classfile/classFileParser.hpp" 28 #include "classfile/classLoader.hpp" 29 #include "classfile/classLoaderData.inline.hpp" 30 #include "classfile/javaAssertions.hpp" 31 #include "classfile/javaClasses.hpp" 32 #include "classfile/javaClasses.inline.hpp" 33 #include "classfile/moduleEntry.hpp" 34 #include "classfile/modules.hpp" 35 #include "classfile/packageEntry.hpp" 36 #include "classfile/stringTable.hpp" 37 #include "classfile/symbolTable.hpp" 38 #include "classfile/systemDictionary.hpp" 39 #include "classfile/vmSymbols.hpp" 40 #include "logging/log.hpp" 41 #include "logging/logStream.hpp" 42 #include "memory/resourceArea.hpp" 43 #include "oops/instanceKlass.hpp" 44 #include "runtime/arguments.hpp" 45 #include "runtime/handles.inline.hpp" 46 #include "runtime/javaCalls.hpp" 47 #include "runtime/jniHandles.inline.hpp" 48 #include "runtime/reflection.hpp" 49 #include "utilities/stringUtils.hpp" 50 #include "utilities/utf8.hpp" 51 52 static bool verify_module_name(const char *module_name) { 53 if (module_name == NULL) return false; 54 int len = (int)strlen(module_name); 55 return (len > 0 && len <= Symbol::max_length()); 56 } 57 58 bool Modules::verify_package_name(const char* package_name) { 59 if (package_name == NULL) return false; 60 int len = (int)strlen(package_name); 61 return (len > 0 && len <= Symbol::max_length() && 62 UTF8::is_legal_utf8((const unsigned char *)package_name, len, false) && 63 ClassFileParser::verify_unqualified_name(package_name, len, 64 ClassFileParser::LegalClass)); 65 } 66 67 static char* get_module_name(oop module, TRAPS) { 68 oop name_oop = java_lang_Module::name(module); 69 if (name_oop == NULL) { 70 THROW_MSG_NULL(vmSymbols::java_lang_NullPointerException(), "Null module name"); 71 } 72 char* module_name = java_lang_String::as_utf8_string(name_oop); 73 if (!verify_module_name(module_name)) { 74 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), 75 err_msg("Invalid module name: %s", 76 module_name != NULL ? module_name : "NULL")); 77 } 78 return module_name; 79 } 80 81 static const char* get_module_version(jstring version) { 82 if (version == NULL) { 83 return NULL; 84 } 85 return java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(version)); 86 } 87 88 ModuleEntryTable* Modules::get_module_entry_table(Handle h_loader) { 89 // This code can be called during start-up, before the classLoader's classLoader data got 90 // created. So, call register_loader() to make sure the classLoader data gets created. 91 ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader); 92 return loader_cld->modules(); 93 } 94 95 static PackageEntryTable* get_package_entry_table(Handle h_loader) { 96 // This code can be called during start-up, before the classLoader's classLoader data got 97 // created. So, call register_loader() to make sure the classLoader data gets created. 98 ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader); 99 return loader_cld->packages(); 100 } 101 102 static ModuleEntry* get_module_entry(jobject module, TRAPS) { 103 oop m = JNIHandles::resolve(module); 104 if (!java_lang_Module::is_instance(m)) { 105 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), 106 "module is not an instance of type java.lang.Module"); 107 } 108 return java_lang_Module::module_entry(m); 109 } 110 111 112 static PackageEntry* get_locked_package_entry(ModuleEntry* module_entry, const char* package_name, TRAPS) { 113 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 114 assert(package_name != NULL, "Precondition"); 115 TempNewSymbol pkg_symbol = SymbolTable::new_symbol(package_name); 116 PackageEntryTable* package_entry_table = module_entry->loader_data()->packages(); 117 assert(package_entry_table != NULL, "Unexpected null package entry table"); 118 PackageEntry* package_entry = package_entry_table->locked_lookup_only(pkg_symbol); 119 assert(package_entry == NULL || package_entry->module() == module_entry, "Unexpectedly found a package linked to another module"); 120 return package_entry; 121 } 122 123 static PackageEntry* get_package_entry_by_name(Symbol* package, 124 Handle h_loader, 125 TRAPS) { 126 if (package != NULL) { 127 ResourceMark rm(THREAD); 128 if (Modules::verify_package_name(package->as_C_string())) { 129 PackageEntryTable* const package_entry_table = 130 get_package_entry_table(h_loader); 131 assert(package_entry_table != NULL, "Unexpected null package entry table"); 132 return package_entry_table->lookup_only(package); 133 } 134 } 135 return NULL; 136 } 137 138 bool Modules::is_package_defined(Symbol* package, Handle h_loader, TRAPS) { 139 PackageEntry* res = get_package_entry_by_name(package, h_loader, CHECK_false); 140 return res != NULL; 141 } 142 143 static void define_javabase_module(jobject module, jstring version, 144 jstring location, const char* const* packages, 145 jsize num_packages, TRAPS) { 146 ResourceMark rm(THREAD); 147 148 Handle module_handle(THREAD, JNIHandles::resolve(module)); 149 150 // Obtain java.base's module version 151 const char* module_version = get_module_version(version); 152 TempNewSymbol version_symbol; 153 if (module_version != NULL) { 154 version_symbol = SymbolTable::new_symbol(module_version); 155 } else { 156 version_symbol = NULL; 157 } 158 159 // Obtain java.base's location 160 const char* module_location = NULL; 161 TempNewSymbol location_symbol = NULL; 162 if (location != NULL) { 163 module_location = 164 java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(location)); 165 if (module_location != NULL) { 166 location_symbol = SymbolTable::new_symbol(module_location); 167 } 168 } 169 170 171 // Check that the packages are syntactically ok. 172 GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages); 173 for (int x = 0; x < num_packages; x++) { 174 const char *package_name = packages[x]; 175 if (!Modules::verify_package_name(package_name)) { 176 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 177 err_msg("Invalid package name: %s for module: " JAVA_BASE_NAME, package_name)); 178 } 179 Symbol* pkg_symbol = SymbolTable::new_symbol(package_name); 180 pkg_list->append(pkg_symbol); 181 } 182 183 // Validate java_base's loader is the boot loader. 184 oop loader = java_lang_Module::loader(module_handle()); 185 if (loader != NULL) { 186 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 187 "Class loader must be the boot class loader"); 188 } 189 Handle h_loader(THREAD, loader); 190 191 // Ensure the boot loader's PackageEntryTable has been created 192 PackageEntryTable* package_table = get_package_entry_table(h_loader); 193 assert(pkg_list->length() == 0 || package_table != NULL, "Bad package_table"); 194 195 // Ensure java.base's ModuleEntry has been created 196 assert(ModuleEntryTable::javabase_moduleEntry() != NULL, "No ModuleEntry for " JAVA_BASE_NAME); 197 198 bool duplicate_javabase = false; 199 { 200 MutexLocker m1(THREAD, Module_lock); 201 202 if (ModuleEntryTable::javabase_defined()) { 203 duplicate_javabase = true; 204 } else { 205 206 // Verify that all java.base packages created during bootstrapping are in 207 // pkg_list. If any are not in pkg_list, than a non-java.base class was 208 // loaded erroneously pre java.base module definition. 209 package_table->verify_javabase_packages(pkg_list); 210 211 // loop through and add any new packages for java.base 212 for (int x = 0; x < pkg_list->length(); x++) { 213 // Some of java.base's packages were added early in bootstrapping, ignore duplicates. 214 package_table->locked_create_entry_if_not_exist(pkg_list->at(x), 215 ModuleEntryTable::javabase_moduleEntry()); 216 assert(package_table->locked_lookup_only(pkg_list->at(x)) != NULL, 217 "Unable to create a " JAVA_BASE_NAME " package entry"); 218 // Unable to have a GrowableArray of TempNewSymbol. Must decrement the refcount of 219 // the Symbol* that was created above for each package. The refcount was incremented 220 // by SymbolTable::new_symbol and as well by the PackageEntry creation. 221 pkg_list->at(x)->decrement_refcount(); 222 } 223 224 // Finish defining java.base's ModuleEntry 225 ModuleEntryTable::finalize_javabase(module_handle, version_symbol, location_symbol); 226 } 227 } 228 if (duplicate_javabase) { 229 THROW_MSG(vmSymbols::java_lang_InternalError(), 230 "Module " JAVA_BASE_NAME " is already defined"); 231 } 232 233 // Only the thread that actually defined the base module will get here, 234 // so no locking is needed. 235 236 // Patch any previously loaded class's module field with java.base's java.lang.Module. 237 ModuleEntryTable::patch_javabase_entries(module_handle); 238 239 log_info(module, load)(JAVA_BASE_NAME " location: %s", 240 module_location != NULL ? module_location : "NULL"); 241 log_debug(module)("define_javabase_module(): Definition of module: " 242 JAVA_BASE_NAME ", version: %s, location: %s, package #: %d", 243 module_version != NULL ? module_version : "NULL", 244 module_location != NULL ? module_location : "NULL", 245 pkg_list->length()); 246 247 // packages defined to java.base 248 if (log_is_enabled(Trace, module)) { 249 for (int x = 0; x < pkg_list->length(); x++) { 250 log_trace(module)("define_javabase_module(): creation of package %s for module " JAVA_BASE_NAME, 251 (pkg_list->at(x))->as_C_string()); 252 } 253 } 254 } 255 256 // Caller needs ResourceMark. 257 void throw_dup_pkg_exception(const char* module_name, PackageEntry* package, TRAPS) { 258 const char* package_name = package->name()->as_C_string(); 259 if (package->module()->is_named()) { 260 THROW_MSG(vmSymbols::java_lang_IllegalStateException(), 261 err_msg("Package %s for module %s is already in another module, %s, defined to the class loader", 262 package_name, module_name, package->module()->name()->as_C_string())); 263 } else { 264 THROW_MSG(vmSymbols::java_lang_IllegalStateException(), 265 err_msg("Package %s for module %s is already in the unnamed module defined to the class loader", 266 package_name, module_name)); 267 } 268 } 269 270 void Modules::define_module(jobject module, jboolean is_open, jstring version, 271 jstring location, const char* const* packages, 272 jsize num_packages, TRAPS) { 273 ResourceMark rm(THREAD); 274 275 if (module == NULL) { 276 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object"); 277 } 278 279 if (num_packages < 0) { 280 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 281 "num_packages must be >= 0"); 282 } 283 284 if (packages == NULL && num_packages > 0) { 285 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 286 "num_packages should be zero if packages is null"); 287 } 288 289 Handle module_handle(THREAD, JNIHandles::resolve(module)); 290 if (!java_lang_Module::is_instance(module_handle())) { 291 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 292 "module is not an instance of type java.lang.Module"); 293 } 294 295 char* module_name = get_module_name(module_handle(), CHECK); 296 if (module_name == NULL) { 297 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 298 "Module name cannot be null"); 299 } 300 301 // Special handling of java.base definition 302 if (strcmp(module_name, JAVA_BASE_NAME) == 0) { 303 assert(is_open == JNI_FALSE, "java.base module cannot be open"); 304 define_javabase_module(module, version, location, packages, num_packages, CHECK); 305 return; 306 } 307 308 const char* module_version = get_module_version(version); 309 310 oop loader = java_lang_Module::loader(module_handle()); 311 // Make sure loader is not the jdk.internal.reflect.DelegatingClassLoader. 312 if (loader != java_lang_ClassLoader::non_reflection_class_loader(loader)) { 313 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 314 "Class loader is an invalid delegating class loader"); 315 } 316 Handle h_loader = Handle(THREAD, loader); 317 // define_module can be called during start-up, before the class loader's ClassLoaderData 318 // has been created. SystemDictionary::register_loader ensures creation, if needed. 319 ClassLoaderData* loader_data = SystemDictionary::register_loader(h_loader); 320 assert(loader_data != NULL, "class loader data shouldn't be null"); 321 322 // Check that the list of packages has no duplicates and that the 323 // packages are syntactically ok. 324 GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages); 325 for (int x = 0; x < num_packages; x++) { 326 const char* package_name = packages[x]; 327 if (!verify_package_name(package_name)) { 328 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 329 err_msg("Invalid package name: %s for module: %s", 330 package_name, module_name)); 331 } 332 333 // Only modules defined to either the boot or platform class loader, can define a "java/" package. 334 if (!h_loader.is_null() && 335 !SystemDictionary::is_platform_class_loader(h_loader()) && 336 (strncmp(package_name, JAVAPKG, JAVAPKG_LEN) == 0 && 337 (package_name[JAVAPKG_LEN] == JVM_SIGNATURE_SLASH || package_name[JAVAPKG_LEN] == '\0'))) { 338 const char* class_loader_name = loader_data->loader_name_and_id(); 339 size_t pkg_len = strlen(package_name); 340 char* pkg_name = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, pkg_len + 1); 341 strncpy(pkg_name, package_name, pkg_len + 1); 342 StringUtils::replace_no_expand(pkg_name, "/", "."); 343 const char* msg_text1 = "Class loader (instance of): "; 344 const char* msg_text2 = " tried to define prohibited package name: "; 345 size_t len = strlen(msg_text1) + strlen(class_loader_name) + strlen(msg_text2) + pkg_len + 1; 346 char* message = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len); 347 jio_snprintf(message, len, "%s%s%s%s", msg_text1, class_loader_name, msg_text2, pkg_name); 348 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), message); 349 } 350 351 Symbol* pkg_symbol = SymbolTable::new_symbol(package_name); 352 pkg_list->append(pkg_symbol); 353 } 354 355 ModuleEntryTable* module_table = get_module_entry_table(h_loader); 356 assert(module_table != NULL, "module entry table shouldn't be null"); 357 358 // Create symbol* entry for module name. 359 TempNewSymbol module_symbol = SymbolTable::new_symbol(module_name); 360 361 bool dupl_modules = false; 362 363 // Create symbol* entry for module version. 364 TempNewSymbol version_symbol; 365 if (module_version != NULL) { 366 version_symbol = SymbolTable::new_symbol(module_version); 367 } else { 368 version_symbol = NULL; 369 } 370 371 // Create symbol* entry for module location. 372 const char* module_location = NULL; 373 TempNewSymbol location_symbol = NULL; 374 if (location != NULL) { 375 module_location = 376 java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(location)); 377 if (module_location != NULL) { 378 location_symbol = SymbolTable::new_symbol(module_location); 379 } 380 } 381 382 PackageEntryTable* package_table = NULL; 383 PackageEntry* existing_pkg = NULL; 384 { 385 MutexLocker ml(THREAD, Module_lock); 386 387 if (num_packages > 0) { 388 package_table = get_package_entry_table(h_loader); 389 assert(package_table != NULL, "Missing package_table"); 390 391 // Check that none of the packages exist in the class loader's package table. 392 for (int x = 0; x < pkg_list->length(); x++) { 393 existing_pkg = package_table->locked_lookup_only(pkg_list->at(x)); 394 if (existing_pkg != NULL) { 395 // This could be because the module was already defined. If so, 396 // report that error instead of the package error. 397 if (module_table->lookup_only(module_symbol) != NULL) { 398 dupl_modules = true; 399 } 400 break; 401 } 402 } 403 } // if (num_packages > 0)... 404 405 // Add the module and its packages. 406 if (!dupl_modules && existing_pkg == NULL) { 407 if (module_table->lookup_only(module_symbol) == NULL) { 408 // Create the entry for this module in the class loader's module entry table. 409 ModuleEntry* module_entry = module_table->locked_create_entry(module_handle, 410 (is_open == JNI_TRUE), module_symbol, 411 version_symbol, location_symbol, loader_data); 412 assert(module_entry != NULL, "module_entry creation failed"); 413 414 // Add the packages. 415 assert(pkg_list->length() == 0 || package_table != NULL, "Bad package table"); 416 for (int y = 0; y < pkg_list->length(); y++) { 417 package_table->locked_create_entry(pkg_list->at(y), module_entry); 418 419 // Unable to have a GrowableArray of TempNewSymbol. Must decrement the refcount of 420 // the Symbol* that was created above for each package. The refcount was incremented 421 // by SymbolTable::new_symbol and as well by the PackageEntry creation. 422 pkg_list->at(y)->decrement_refcount(); 423 } 424 425 // Store pointer to ModuleEntry record in java.lang.Module object. 426 java_lang_Module::set_module_entry(module_handle(), module_entry); 427 } else { 428 dupl_modules = true; 429 } 430 } 431 } // Release the lock 432 433 // any errors ? 434 if (dupl_modules) { 435 THROW_MSG(vmSymbols::java_lang_IllegalStateException(), 436 err_msg("Module %s is already defined", module_name)); 437 } else if (existing_pkg != NULL) { 438 throw_dup_pkg_exception(module_name, existing_pkg, CHECK); 439 } 440 441 log_info(module, load)("%s location: %s", module_name, 442 module_location != NULL ? module_location : "NULL"); 443 LogTarget(Debug, module) lt; 444 if (lt.is_enabled()) { 445 LogStream ls(lt); 446 ls.print("define_module(): creation of module: %s, version: %s, location: %s, ", 447 module_name, module_version != NULL ? module_version : "NULL", 448 module_location != NULL ? module_location : "NULL"); 449 loader_data->print_value_on(&ls); 450 ls.print_cr(", package #: %d", pkg_list->length()); 451 for (int y = 0; y < pkg_list->length(); y++) { 452 log_trace(module)("define_module(): creation of package %s for module %s", 453 (pkg_list->at(y))->as_C_string(), module_name); 454 } 455 } 456 457 // If the module is defined to the boot loader and an exploded build is being 458 // used, prepend <java.home>/modules/modules_name to the system boot class path. 459 if (h_loader.is_null() && !ClassLoader::has_jrt_entry()) { 460 ClassLoader::add_to_exploded_build_list(module_symbol, CHECK); 461 } 462 } 463 464 void Modules::set_bootloader_unnamed_module(jobject module, TRAPS) { 465 ResourceMark rm(THREAD); 466 467 if (module == NULL) { 468 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object"); 469 } 470 Handle module_handle(THREAD, JNIHandles::resolve(module)); 471 if (!java_lang_Module::is_instance(module_handle())) { 472 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 473 "module is not an instance of type java.lang.Module"); 474 } 475 476 // Ensure that this is an unnamed module 477 oop name = java_lang_Module::name(module_handle()); 478 if (name != NULL) { 479 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 480 "boot loader's unnamed module's java.lang.Module has a name"); 481 } 482 483 // Validate java_base's loader is the boot loader. 484 oop loader = java_lang_Module::loader(module_handle()); 485 if (loader != NULL) { 486 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 487 "Class loader must be the boot class loader"); 488 } 489 Handle h_loader(THREAD, loader); 490 491 log_debug(module)("set_bootloader_unnamed_module(): recording unnamed module for boot loader"); 492 493 // Set java.lang.Module for the boot loader's unnamed module 494 ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data(); 495 ModuleEntry* unnamed_module = boot_loader_data->unnamed_module(); 496 assert(unnamed_module != NULL, "boot loader's unnamed ModuleEntry not defined"); 497 unnamed_module->set_module(boot_loader_data->add_handle(module_handle)); 498 // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module object. 499 java_lang_Module::set_module_entry(module_handle(), unnamed_module); 500 } 501 502 void Modules::add_module_exports(jobject from_module, const char* package_name, jobject to_module, TRAPS) { 503 if (package_name == NULL) { 504 THROW_MSG(vmSymbols::java_lang_NullPointerException(), 505 "package is null"); 506 } 507 if (from_module == NULL) { 508 THROW_MSG(vmSymbols::java_lang_NullPointerException(), 509 "from_module is null"); 510 } 511 ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK); 512 if (from_module_entry == NULL) { 513 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 514 "from_module cannot be found"); 515 } 516 517 // All packages in unnamed and open modules are exported by default. 518 if (!from_module_entry->is_named() || from_module_entry->is_open()) return; 519 520 ModuleEntry* to_module_entry; 521 if (to_module == NULL) { 522 to_module_entry = NULL; // It's an unqualified export. 523 } else { 524 to_module_entry = get_module_entry(to_module, CHECK); 525 if (to_module_entry == NULL) { 526 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 527 "to_module is invalid"); 528 } 529 } 530 531 PackageEntry* package_entry = NULL; 532 { 533 MutexLocker ml(THREAD, Module_lock); 534 package_entry = get_locked_package_entry(from_module_entry, package_name, CHECK); 535 // Do nothing if modules are the same 536 // If the package is not found we'll throw an exception later 537 if (from_module_entry != to_module_entry && 538 package_entry != NULL) { 539 package_entry->set_exported(to_module_entry); 540 } 541 } 542 543 // Handle errors and logging outside locked section 544 if (package_entry == NULL) { 545 ResourceMark rm(THREAD); 546 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 547 err_msg("Package %s not found in from_module %s", 548 package_name != NULL ? package_name : "", 549 from_module_entry->name()->as_C_string())); 550 } 551 552 if (log_is_enabled(Debug, module)) { 553 ResourceMark rm(THREAD); 554 log_debug(module)("add_module_exports(): package %s in module %s is exported to module %s", 555 package_entry->name()->as_C_string(), 556 from_module_entry->name()->as_C_string(), 557 to_module_entry == NULL ? "NULL" : 558 to_module_entry->is_named() ? 559 to_module_entry->name()->as_C_string() : UNNAMED_MODULE); 560 } 561 } 562 563 564 void Modules::add_module_exports_qualified(jobject from_module, const char* package, 565 jobject to_module, TRAPS) { 566 if (to_module == NULL) { 567 THROW_MSG(vmSymbols::java_lang_NullPointerException(), 568 "to_module is null"); 569 } 570 add_module_exports(from_module, package, to_module, CHECK); 571 } 572 573 void Modules::add_reads_module(jobject from_module, jobject to_module, TRAPS) { 574 if (from_module == NULL) { 575 THROW_MSG(vmSymbols::java_lang_NullPointerException(), 576 "from_module is null"); 577 } 578 579 ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK); 580 if (from_module_entry == NULL) { 581 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 582 "from_module is not valid"); 583 } 584 585 ModuleEntry* to_module_entry; 586 if (to_module != NULL) { 587 to_module_entry = get_module_entry(to_module, CHECK); 588 if (to_module_entry == NULL) { 589 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 590 "to_module is invalid"); 591 } 592 } else { 593 to_module_entry = NULL; 594 } 595 596 ResourceMark rm(THREAD); 597 log_debug(module)("add_reads_module(): Adding read from module %s to module %s", 598 from_module_entry->is_named() ? 599 from_module_entry->name()->as_C_string() : UNNAMED_MODULE, 600 to_module_entry == NULL ? "all unnamed" : 601 (to_module_entry->is_named() ? 602 to_module_entry->name()->as_C_string() : UNNAMED_MODULE)); 603 604 // if modules are the same or if from_module is unnamed then no need to add the read. 605 if (from_module_entry != to_module_entry && from_module_entry->is_named()) { 606 from_module_entry->add_read(to_module_entry); 607 } 608 } 609 610 // This method is called by JFR and JNI. 611 jobject Modules::get_module(jclass clazz, TRAPS) { 612 assert(ModuleEntryTable::javabase_defined(), 613 "Attempt to call get_module before " JAVA_BASE_NAME " is defined"); 614 615 if (clazz == NULL) { 616 THROW_MSG_(vmSymbols::java_lang_NullPointerException(), 617 "class is null", JNI_FALSE); 618 } 619 oop mirror = JNIHandles::resolve_non_null(clazz); 620 if (mirror == NULL) { 621 log_debug(module)("get_module(): no mirror, returning NULL"); 622 return NULL; 623 } 624 if (!java_lang_Class::is_instance(mirror)) { 625 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), 626 "Invalid class", JNI_FALSE); 627 } 628 629 oop module = java_lang_Class::module(mirror); 630 631 assert(module != NULL, "java.lang.Class module field not set"); 632 assert(java_lang_Module::is_instance(module), "module is not an instance of type java.lang.Module"); 633 634 LogTarget(Debug,module) lt; 635 if (lt.is_enabled()) { 636 ResourceMark rm(THREAD); 637 LogStream ls(lt); 638 Klass* klass = java_lang_Class::as_Klass(mirror); 639 oop module_name = java_lang_Module::name(module); 640 if (module_name != NULL) { 641 ls.print("get_module(): module "); 642 java_lang_String::print(module_name, tty); 643 } else { 644 ls.print("get_module(): Unamed Module"); 645 } 646 if (klass != NULL) { 647 ls.print_cr(" for class %s", klass->external_name()); 648 } else { 649 ls.print_cr(" for primitive class"); 650 } 651 } 652 653 return JNIHandles::make_local(THREAD, module); 654 } 655 656 jobject Modules::get_named_module(Handle h_loader, const char* package_name, TRAPS) { 657 assert(ModuleEntryTable::javabase_defined(), 658 "Attempt to call get_named_module before " JAVA_BASE_NAME " is defined"); 659 assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()), 660 "Class loader is not a subclass of java.lang.ClassLoader"); 661 assert(package_name != NULL, "the package_name should not be NULL"); 662 663 if (strlen(package_name) == 0) { 664 return NULL; 665 } 666 TempNewSymbol package_sym = SymbolTable::new_symbol(package_name); 667 const PackageEntry* const pkg_entry = 668 get_package_entry_by_name(package_sym, h_loader, THREAD); 669 const ModuleEntry* const module_entry = (pkg_entry != NULL ? pkg_entry->module() : NULL); 670 671 if (module_entry != NULL && module_entry->module() != NULL && module_entry->is_named()) { 672 return JNIHandles::make_local(THREAD, module_entry->module()); 673 } 674 return NULL; 675 } 676 677 // Export package in module to all unnamed modules. 678 void Modules::add_module_exports_to_all_unnamed(jobject module, const char* package_name, TRAPS) { 679 if (module == NULL) { 680 THROW_MSG(vmSymbols::java_lang_NullPointerException(), 681 "module is null"); 682 } 683 if (package_name == NULL) { 684 THROW_MSG(vmSymbols::java_lang_NullPointerException(), 685 "package is null"); 686 } 687 ModuleEntry* module_entry = get_module_entry(module, CHECK); 688 if (module_entry == NULL) { 689 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 690 "module is invalid"); 691 } 692 693 // No-op for unnamed module and open modules 694 if (!module_entry->is_named() || module_entry->is_open()) 695 return; 696 697 PackageEntry* package_entry = NULL; 698 { 699 MutexLocker m1(THREAD, Module_lock); 700 package_entry = get_locked_package_entry(module_entry, package_name, CHECK); 701 702 // Mark package as exported to all unnamed modules. 703 if (package_entry != NULL) { 704 package_entry->set_is_exported_allUnnamed(); 705 } 706 } 707 708 // Handle errors and logging outside locked section 709 if (package_entry == NULL) { 710 ResourceMark rm(THREAD); 711 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 712 err_msg("Package %s not found in module %s", 713 package_name != NULL ? package_name : "", 714 module_entry->name()->as_C_string())); 715 } 716 717 if (log_is_enabled(Debug, module)) { 718 ResourceMark rm(THREAD); 719 log_debug(module)("add_module_exports_to_all_unnamed(): package %s in module" 720 " %s is exported to all unnamed modules", 721 package_entry->name()->as_C_string(), 722 module_entry->name()->as_C_string()); 723 } 724 }