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