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