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