1 /*
   2 * Copyright (c) 2016, 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 "classfile/classFileParser.hpp"
  27 #include "classfile/classLoader.hpp"
  28 #include "classfile/classLoaderData.inline.hpp"
  29 #include "classfile/javaAssertions.hpp"
  30 #include "classfile/javaClasses.hpp"
  31 #include "classfile/javaClasses.inline.hpp"
  32 #include "classfile/moduleEntry.hpp"
  33 #include "classfile/modules.hpp"
  34 #include "classfile/packageEntry.hpp"
  35 #include "classfile/stringTable.hpp"
  36 #include "classfile/symbolTable.hpp"
  37 #include "classfile/vmSymbols.hpp"
  38 #include "logging/log.hpp"
  39 #include "memory/resourceArea.hpp"
  40 #include "oops/instanceKlass.hpp"
  41 #include "oops/objArrayKlass.hpp"
  42 #include "oops/objArrayOop.inline.hpp"
  43 #include "runtime/arguments.hpp"
  44 #include "runtime/handles.inline.hpp"
  45 #include "runtime/javaCalls.hpp"
  46 #include "runtime/reflection.hpp"
  47 #include "utilities/utf8.hpp"
  48 
  49 static bool verify_module_name(char *module_name) {
  50   if (module_name == NULL) return false;
  51   int len = (int)strlen(module_name);
  52   return (len > 0 && len <= Symbol::max_length() &&
  53     UTF8::is_legal_utf8((unsigned char *)module_name, len, false) &&
  54     ClassFileParser::verify_unqualified_name(module_name, len,
  55     ClassFileParser::LegalModule));
  56 }
  57 
  58 bool Modules::verify_package_name(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((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_reflect_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 static ModuleEntryTable* get_module_entry_table(Handle h_loader, TRAPS) {
  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, CHECK_NULL);
  92   return loader_cld->modules();
  93 }
  94 
  95 static PackageEntryTable* get_package_entry_table(Handle h_loader, TRAPS) {
  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, CHECK_NULL);
  99   return loader_cld->packages();
 100 }
 101 
 102 static ModuleEntry* get_module_entry(jobject module, TRAPS) {
 103   Handle module_h(THREAD, JNIHandles::resolve(module));
 104   if (!java_lang_reflect_Module::is_instance(module_h())) {
 105     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
 106                    "module is not an instance of type java.lang.reflect.Module");
 107   }
 108   return java_lang_reflect_Module::module_entry(module_h(), CHECK_NULL);
 109 }
 110 
 111 static PackageEntry* get_package_entry(ModuleEntry* module_entry, jstring package, TRAPS) {
 112   ResourceMark rm(THREAD);
 113   if (package == NULL) return NULL;
 114   const char *package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package));
 115   if (package_name == NULL) return NULL;
 116   TempNewSymbol pkg_symbol = SymbolTable::new_symbol(package_name, CHECK_NULL);
 117   PackageEntryTable* package_entry_table = module_entry->loader_data()->packages();
 118   assert(package_entry_table != NULL, "Unexpected null package entry table");
 119   return package_entry_table->lookup_only(pkg_symbol);
 120 }
 121 
 122 static PackageEntry* get_package_entry_by_name(Symbol* package,
 123                                                Handle h_loader,
 124                                                TRAPS) {
 125   if (package != NULL) {
 126     ResourceMark rm(THREAD);
 127     if (Modules::verify_package_name(package->as_C_string())) {
 128       PackageEntryTable* const package_entry_table =
 129         get_package_entry_table(h_loader, CHECK_NULL);
 130       assert(package_entry_table != NULL, "Unexpected null package entry table");
 131       return package_entry_table->lookup_only(package);
 132     }
 133   }
 134   return NULL;
 135 }
 136 
 137 bool Modules::is_package_defined(Symbol* package, Handle h_loader, TRAPS) {
 138   PackageEntry* res = get_package_entry_by_name(package, h_loader, CHECK_false);
 139   return res != NULL;
 140 }
 141 
 142 static void define_javabase_module(jobject module, jstring version,
 143                                    jstring location, jobjectArray packages, TRAPS) {
 144   ResourceMark rm(THREAD);
 145 
 146   Handle module_handle(THREAD, JNIHandles::resolve(module));
 147 
 148   // Obtain java.base's module version
 149   const char* module_version = get_module_version(version);
 150   TempNewSymbol version_symbol;
 151   if (module_version != NULL) {
 152     version_symbol = SymbolTable::new_symbol(module_version, CHECK);
 153   } else {
 154     version_symbol = NULL;
 155   }
 156 
 157   // Obtain java.base's location
 158   const char* module_location = NULL;
 159   TempNewSymbol location_symbol = NULL;
 160   if (location != NULL) {
 161     module_location =
 162       java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(location));
 163     if (module_location != NULL) {
 164       location_symbol = SymbolTable::new_symbol(module_location, CHECK);
 165     }
 166   }
 167 
 168   objArrayOop packages_oop = objArrayOop(JNIHandles::resolve(packages));
 169   objArrayHandle packages_h(THREAD, packages_oop);
 170   int num_packages = (packages_h == NULL ? 0 : packages_h->length());
 171 
 172   // Check that the list of packages has no duplicates and that the
 173   // packages are syntactically ok.
 174   GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages);
 175   for (int x = 0; x < num_packages; x++) {
 176     oop string_obj = packages_h->obj_at(x);
 177 
 178     if (string_obj == NULL || !string_obj->is_a(SystemDictionary::String_klass())) {
 179       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 180                 "Bad package name for module: java.base");
 181     }
 182     char *package_name = java_lang_String::as_utf8_string(string_obj);
 183     if (!Modules::verify_package_name(package_name)) {
 184       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 185                 err_msg("Invalid package name: %s for module: java.base", package_name));
 186     }
 187     Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, CHECK);
 188     // append_if_missing() returns FALSE if entry already exists.
 189     if (!pkg_list->append_if_missing(pkg_symbol)) {
 190       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 191                 err_msg("Duplicate package name: %s for module java.base",
 192                         package_name));
 193     }
 194   }
 195 
 196   // Validate java_base's loader is the boot loader.
 197   oop loader = java_lang_reflect_Module::loader(module_handle());
 198   if (loader != NULL) {
 199     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 200               "Class loader must be the boot class loader");
 201   }
 202   Handle h_loader = Handle(THREAD, loader);
 203 
 204   // Ensure the boot loader's PackageEntryTable has been created
 205   PackageEntryTable* package_table = get_package_entry_table(h_loader, CHECK);
 206   assert(pkg_list->length() == 0 || package_table != NULL, "Bad package_table");
 207 
 208   // Ensure java.base's ModuleEntry has been created
 209   assert(ModuleEntryTable::javabase_moduleEntry() != NULL, "No ModuleEntry for java.base");
 210 
 211   bool duplicate_javabase = false;
 212   {
 213     MutexLocker m1(Module_lock, THREAD);
 214 
 215     if (ModuleEntryTable::javabase_defined()) {
 216       duplicate_javabase = true;
 217     } else {
 218 
 219       // Verify that all java.base packages created during bootstrapping are in
 220       // pkg_list.  If any are not in pkg_list, than a non-java.base class was
 221       // loaded erroneously pre java.base module definition.
 222       package_table->verify_javabase_packages(pkg_list);
 223 
 224       // loop through and add any new packages for java.base
 225       PackageEntry* pkg;
 226       for (int x = 0; x < pkg_list->length(); x++) {
 227         // Some of java.base's packages were added early in bootstrapping, ignore duplicates.
 228         if (package_table->lookup_only(pkg_list->at(x)) == NULL) {
 229           pkg = package_table->locked_create_entry_or_null(pkg_list->at(x), ModuleEntryTable::javabase_moduleEntry());
 230           assert(pkg != NULL, "Unable to create a java.base package entry");
 231         }
 232         // Unable to have a GrowableArray of TempNewSymbol.  Must decrement the refcount of
 233         // the Symbol* that was created above for each package. The refcount was incremented
 234         // by SymbolTable::new_symbol and as well by the PackageEntry creation.
 235         pkg_list->at(x)->decrement_refcount();
 236       }
 237 
 238       // Finish defining java.base's ModuleEntry
 239       ModuleEntryTable::finalize_javabase(module_handle, version_symbol, location_symbol);
 240     }
 241   }
 242   if (duplicate_javabase) {
 243     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 244               "Module java.base is already defined");
 245   }
 246 
 247   log_debug(modules)("define_javabase_module(): Definition of module: java.base,"
 248                      " version: %s, location: %s, package #: %d",
 249                      module_version != NULL ? module_version : "NULL",
 250                      module_location != NULL ? module_location : "NULL",
 251                      pkg_list->length());
 252 
 253   // packages defined to java.base
 254   for (int x = 0; x < pkg_list->length(); x++) {
 255     log_trace(modules)("define_javabase_module(): creation of package %s for module java.base",
 256                        (pkg_list->at(x))->as_C_string());
 257   }
 258 }
 259 
 260 void Modules::define_module(jobject module, jstring version,
 261                             jstring location, jobjectArray packages, TRAPS) {
 262   ResourceMark rm(THREAD);
 263 
 264   if (module == NULL) {
 265     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
 266   }
 267   Handle module_handle(THREAD, JNIHandles::resolve(module));
 268   if (!java_lang_reflect_Module::is_instance(module_handle())) {
 269     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 270               "module is not an instance of type java.lang.reflect.Module");
 271   }
 272 
 273   char* module_name = get_module_name(module_handle(), CHECK);
 274   if (module_name == NULL) {
 275     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 276               "Module name cannot be null");
 277   }
 278 
 279   // Special handling of java.base definition
 280   if (strcmp(module_name, "java.base") == 0) {
 281     define_javabase_module(module, version, location, packages, CHECK);
 282     return;
 283   }
 284 
 285   const char* module_version = get_module_version(version);
 286 
 287   objArrayOop packages_oop = objArrayOop(JNIHandles::resolve(packages));
 288   objArrayHandle packages_h(THREAD, packages_oop);
 289   int num_packages = (packages_h == NULL ? 0 : packages_h->length());
 290 
 291   // Check that the list of packages has no duplicates and that the
 292   // packages are syntactically ok.
 293   GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages);
 294   for (int x = 0; x < num_packages; x++) {
 295     oop string_obj = packages_h->obj_at(x);
 296 
 297     if (string_obj == NULL || !string_obj->is_a(SystemDictionary::String_klass())) {
 298       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 299                 err_msg("Bad package name for module: %s", module_name));
 300     }
 301     char *package_name = java_lang_String::as_utf8_string(string_obj);
 302     if (!verify_package_name(package_name)) {
 303       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 304                 err_msg("Invalid package name: %s for module: %s",
 305                         package_name, module_name));
 306     }
 307     Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, CHECK);
 308     // append_if_missing() returns FALSE if entry already exists.
 309     if (!pkg_list->append_if_missing(pkg_symbol)) {
 310       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 311                 err_msg("Duplicate package name: %s for module %s",
 312                         package_name, module_name));
 313     }
 314   }
 315 
 316   oop loader = java_lang_reflect_Module::loader(module_handle());
 317   // Make sure loader is not the sun.reflect.DelegatingClassLoader.
 318   if (loader != java_lang_ClassLoader::non_reflection_class_loader(loader)) {
 319     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 320               "Class loader is an invalid delegating class loader");
 321   }
 322   Handle h_loader = Handle(THREAD, loader);
 323 
 324   // Check that loader is a subclass of java.lang.ClassLoader.
 325   if (loader != NULL && !java_lang_ClassLoader::is_subclass(h_loader->klass())) {
 326     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 327               "Class loader is not a subclass of java.lang.ClassLoader");
 328   }
 329 
 330   ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK);
 331   assert(module_table != NULL, "module entry table shouldn't be null");
 332 
 333   // Create symbol* entry for module name.
 334   TempNewSymbol module_symbol = SymbolTable::new_symbol(module_name, CHECK);
 335 
 336   int dupl_pkg_index = -1;
 337   bool dupl_modules = false;
 338 
 339   // Create symbol* entry for module version.
 340   TempNewSymbol version_symbol;
 341   if (module_version != NULL) {
 342     version_symbol = SymbolTable::new_symbol(module_version, CHECK);
 343   } else {
 344     version_symbol = NULL;
 345   }
 346 
 347   // Create symbol* entry for module location.
 348   const char* module_location = NULL;
 349   TempNewSymbol location_symbol = NULL;
 350   if (location != NULL) {
 351     module_location =
 352       java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(location));
 353     if (module_location != NULL) {
 354       location_symbol = SymbolTable::new_symbol(module_location, CHECK);
 355     }
 356   }
 357 
 358   ClassLoaderData* loader_data = ClassLoaderData::class_loader_data_or_null(h_loader());
 359   assert(loader_data != NULL, "class loader data shouldn't be null");
 360 
 361   PackageEntryTable* package_table = NULL;
 362   {
 363     MutexLocker ml(Module_lock, THREAD);
 364 
 365     if (num_packages > 0) {
 366       package_table = get_package_entry_table(h_loader, CHECK);
 367       assert(package_table != NULL, "Missing package_table");
 368 
 369       // Check that none of the packages exist in the class loader's package table.
 370       for (int x = 0; x < pkg_list->length(); x++) {
 371         if (package_table->lookup_only(pkg_list->at(x)) != NULL) {
 372           // This could be because the module was already defined.  If so,
 373           // report that error instead of the package error.
 374           if (module_table->lookup_only(module_symbol) != NULL) {
 375             dupl_modules = true;
 376           } else {
 377             dupl_pkg_index = x;
 378           }
 379           break;
 380         }
 381       }
 382     }  // if (num_packages > 0)...
 383 
 384     // Add the module and its packages.
 385     if (!dupl_modules && dupl_pkg_index == -1) {
 386       // Create the entry for this module in the class loader's module entry table.
 387 
 388       ModuleEntry* module_entry = module_table->locked_create_entry_or_null(module_handle, module_symbol,
 389                                     version_symbol, location_symbol, loader_data);
 390 
 391       if (module_entry == NULL) {
 392         dupl_modules = true;
 393       } else {
 394         // Add the packages.
 395         assert(pkg_list->length() == 0 || package_table != NULL, "Bad package table");
 396         PackageEntry* pkg;
 397         for (int y = 0; y < pkg_list->length(); y++) {
 398           pkg = package_table->locked_create_entry_or_null(pkg_list->at(y), module_entry);
 399           assert(pkg != NULL, "Unable to create a module's package entry");
 400 
 401           // Unable to have a GrowableArray of TempNewSymbol.  Must decrement the refcount of
 402           // the Symbol* that was created above for each package. The refcount was incremented
 403           // by SymbolTable::new_symbol and as well by the PackageEntry creation.
 404           pkg_list->at(y)->decrement_refcount();
 405         }
 406 
 407         // Store pointer to ModuleEntry record in java.lang.reflect.Module object.
 408         java_lang_reflect_Module::set_module_entry(module_handle(), module_entry);
 409       }
 410     }
 411   }  // Release the lock
 412 
 413   // any errors ?
 414   if (dupl_modules) {
 415      THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 416                err_msg("Module %s is already defined", module_name));
 417   }
 418   if (dupl_pkg_index != -1) {
 419     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 420               err_msg("Package %s for module %s already exists for class loader",
 421                       pkg_list->at(dupl_pkg_index)->as_C_string(), module_name));
 422   }
 423 
 424   if (log_is_enabled(Debug, modules)) {
 425     outputStream* logst = Log(modules)::debug_stream();
 426     logst->print("define_module(): creation of module: %s, version: %s, location: %s, ",
 427                  module_name, module_version != NULL ? module_version : "NULL",
 428                  module_location != NULL ? module_location : "NULL");
 429     loader_data->print_value_on(logst);
 430     logst->print_cr(", package #: %d", pkg_list->length());
 431     for (int y = 0; y < pkg_list->length(); y++) {
 432       log_trace(modules)("define_module(): creation of package %s for module %s",
 433                          (pkg_list->at(y))->as_C_string(), module_name);
 434     }
 435   }
 436 
 437   // If the module is defined to the boot loader and an exploded build is being
 438   // used, prepend <java.home>/modules/modules_name, if it exists, to the system boot class path.
 439   if (loader == NULL &&
 440       !Universe::is_module_initialized() &&
 441       !ClassLoader::has_jrt_entry()) {
 442     ClassLoader::add_to_exploded_build_list(module_symbol, CHECK);
 443   }
 444 }
 445 
 446 void Modules::set_bootloader_unnamed_module(jobject module, TRAPS) {
 447   ResourceMark rm(THREAD);
 448 
 449   if (module == NULL) {
 450     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
 451   }
 452   Handle module_handle(THREAD, JNIHandles::resolve(module));
 453   if (!java_lang_reflect_Module::is_instance(module_handle())) {
 454     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 455               "module is not an instance of type java.lang.reflect.Module");
 456   }
 457 
 458   // Ensure that this is an unnamed module
 459   oop name = java_lang_reflect_Module::name(module_handle());
 460   if (name != NULL) {
 461     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 462               "boot loader's unnamed module's java.lang.reflect.Module has a name");
 463   }
 464 
 465   // Validate java_base's loader is the boot loader.
 466   oop loader = java_lang_reflect_Module::loader(module_handle());
 467   if (loader != NULL) {
 468     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 469               "Class loader must be the boot class loader");
 470   }
 471   Handle h_loader = Handle(THREAD, loader);
 472 
 473   log_debug(modules)("set_bootloader_unnamed_module(): recording unnamed module for boot loader");
 474 
 475   // Ensure the boot loader's PackageEntryTable has been created
 476   ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK);
 477 
 478   // Set java.lang.reflect.Module for the boot loader's unnamed module
 479   ModuleEntry* unnamed_module = module_table->unnamed_module();
 480   assert(unnamed_module != NULL, "boot loader's unnamed ModuleEntry not defined");
 481   unnamed_module->set_module(ClassLoaderData::the_null_class_loader_data()->add_handle(module_handle));
 482   // Store pointer to the ModuleEntry in the unnamed module's java.lang.reflect.Module object.
 483   java_lang_reflect_Module::set_module_entry(module_handle(), unnamed_module);
 484 }
 485 
 486 void Modules::add_module_exports(jobject from_module, jstring package, jobject to_module, TRAPS) {
 487   if (package == NULL) {
 488     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 489               "package is null");
 490   }
 491   if (from_module == NULL) {
 492     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 493               "from_module is null");
 494   }
 495   ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
 496   if (from_module_entry == NULL) {
 497     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 498               "from_module cannot be found");
 499   }
 500 
 501   // All packages in unnamed are exported by default.
 502   if (!from_module_entry->is_named()) return;
 503 
 504   ModuleEntry* to_module_entry;
 505   if (to_module == NULL) {
 506     to_module_entry = NULL;  // It's an unqualified export.
 507   } else {
 508     to_module_entry = get_module_entry(to_module, CHECK);
 509     if (to_module_entry == NULL) {
 510       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 511                 "to_module is invalid");
 512     }
 513   }
 514 
 515   PackageEntry *package_entry = get_package_entry(from_module_entry, package, CHECK);
 516   ResourceMark rm(THREAD);
 517   if (package_entry == NULL) {
 518     const char *package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package));
 519     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 520               err_msg("Package %s not found in from_module %s",
 521                       package_name != NULL ? package_name : "",
 522                       from_module_entry->name()->as_C_string()));
 523   }
 524   if (package_entry->module() != from_module_entry) {
 525     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 526               err_msg("Package: %s found in module %s, not in from_module: %s",
 527                       package_entry->name()->as_C_string(),
 528                       package_entry->module()->name()->as_C_string(),
 529                       from_module_entry->name()->as_C_string()));
 530   }
 531 
 532   log_debug(modules)("add_module_exports(): package %s in module %s is exported to module %s",
 533                      package_entry->name()->as_C_string(),
 534                      from_module_entry->name()->as_C_string(),
 535                      to_module_entry == NULL ? "NULL" :
 536                       to_module_entry->is_named() ?
 537                         to_module_entry->name()->as_C_string() : UNNAMED_MODULE);
 538 
 539   // Do nothing if modules are the same.
 540   if (from_module_entry != to_module_entry) {
 541     package_entry->set_exported(to_module_entry);
 542   }
 543 }
 544 
 545 
 546 void Modules::add_module_exports_qualified(jobject from_module, jstring package,
 547                                            jobject to_module, TRAPS) {
 548   if (to_module == NULL) {
 549     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 550               "to_module is null");
 551   }
 552   add_module_exports(from_module, package, to_module, CHECK);
 553 }
 554 
 555 void Modules::add_reads_module(jobject from_module, jobject to_module, TRAPS) {
 556   if (from_module == NULL) {
 557     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 558               "from_module is null");
 559   }
 560 
 561   ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
 562   if (from_module_entry == NULL) {
 563     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 564               "from_module is not valid");
 565   }
 566 
 567   ModuleEntry* to_module_entry;
 568   if (to_module != NULL) {
 569     to_module_entry = get_module_entry(to_module, CHECK);
 570     if (to_module_entry == NULL) {
 571       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 572                 "to_module is invalid");
 573     }
 574   } else {
 575     to_module_entry = NULL;
 576   }
 577 
 578   ResourceMark rm(THREAD);
 579   log_debug(modules)("add_reads_module(): Adding read from module %s to module %s",
 580                      from_module_entry->is_named() ?
 581                      from_module_entry->name()->as_C_string() : UNNAMED_MODULE,
 582                      to_module_entry == NULL ? "all unnamed" :
 583                        (to_module_entry->is_named() ?
 584                         to_module_entry->name()->as_C_string() : UNNAMED_MODULE));
 585 
 586   // if modules are the same or if from_module is unnamed then no need to add the read.
 587   if (from_module_entry != to_module_entry && from_module_entry->is_named()) {
 588     from_module_entry->add_read(to_module_entry);
 589   }
 590 }
 591 
 592 jboolean Modules::can_read_module(jobject asking_module, jobject target_module, TRAPS) {
 593   if (asking_module == NULL) {
 594     THROW_MSG_(vmSymbols::java_lang_NullPointerException(),
 595                "asking_module is null", JNI_FALSE);
 596   }
 597 
 598   ModuleEntry* asking_module_entry = get_module_entry(asking_module, CHECK_false);
 599   if (asking_module_entry == NULL) {
 600     THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 601                "asking_module is invalid", JNI_FALSE);
 602   }
 603 
 604   // Calling can_read_all_unnamed() with NULL tests if a module is loose.
 605   if (target_module == NULL) {
 606     return asking_module_entry->can_read_all_unnamed();
 607   }
 608 
 609   ModuleEntry* target_module_entry = get_module_entry(target_module, CHECK_false);
 610   if (target_module_entry == NULL) {
 611     THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 612                "target_module is invalid", JNI_FALSE);
 613   }
 614 
 615   ResourceMark rm(THREAD);
 616   log_debug(modules)("can_read_module(): module %s trying to read module %s, allowed = %s",
 617                      asking_module_entry->is_named() ?
 618                        asking_module_entry->name()->as_C_string() : UNNAMED_MODULE,
 619                      target_module_entry->is_named() ?
 620                        target_module_entry->name()->as_C_string() : UNNAMED_MODULE,
 621                      BOOL_TO_STR(asking_module_entry == target_module_entry ||
 622                                  (asking_module_entry->can_read_all_unnamed() &&
 623                                   !target_module_entry->is_named()) ||
 624                                   asking_module_entry->can_read(target_module_entry)));
 625 
 626   // Return true if:
 627   // 1. the modules are the same, or
 628   // 2. the asking_module is unnamed (because unnamed modules read everybody), or
 629   // 3. the asking_module is loose and the target module is unnamed, or
 630   // 4. if can_read() returns true.
 631   if (asking_module_entry == target_module_entry ||
 632       (asking_module_entry->can_read_all_unnamed() && !target_module_entry->is_named())) {
 633     return true;
 634   }
 635   return asking_module_entry->can_read(target_module_entry);
 636 }
 637 
 638 jboolean Modules::is_exported_to_module(jobject from_module, jstring package,
 639                                         jobject to_module, TRAPS) {
 640   if (package == NULL) {
 641     THROW_MSG_(vmSymbols::java_lang_NullPointerException(),
 642                "package is null", JNI_FALSE);
 643   }
 644   if (from_module == NULL) {
 645     THROW_MSG_(vmSymbols::java_lang_NullPointerException(),
 646                "from_module is null", JNI_FALSE);
 647   }
 648   ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK_false);
 649   if (from_module_entry == NULL) {
 650     THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 651                "from_module is invalid", JNI_FALSE);
 652   }
 653   ModuleEntry* to_module_entry;
 654   if (to_module == NULL) {
 655     THROW_MSG_(vmSymbols::java_lang_NullPointerException(),
 656                "to_module is null", JNI_FALSE);
 657   }
 658   to_module_entry = get_module_entry(to_module, CHECK_false);
 659   if (to_module_entry == NULL) {
 660     THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 661                "to_module is invalid", JNI_FALSE);
 662   }
 663 
 664   PackageEntry *package_entry = get_package_entry(from_module_entry, package,
 665                                                   CHECK_false);
 666   ResourceMark rm(THREAD);
 667   if (package_entry == NULL) {
 668     THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 669                err_msg("Package not found in from_module: %s",
 670                        from_module_entry->is_named() ?
 671                          from_module_entry->name()->as_C_string() : UNNAMED_MODULE),
 672                JNI_FALSE);
 673   }
 674   if (package_entry->module() != from_module_entry) {
 675     THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 676                err_msg("Package: %s found in module %s, not in from_module: %s",
 677                        package_entry->name()->as_C_string(),
 678                        package_entry->module()->is_named() ?
 679                          package_entry->module()->name()->as_C_string() : UNNAMED_MODULE,
 680                        from_module_entry->is_named() ?
 681                          from_module_entry->name()->as_C_string() : UNNAMED_MODULE),
 682                JNI_FALSE);
 683   }
 684 
 685   log_debug(modules)("is_exported_to_module: package %s from module %s checking"
 686                      " if exported to module %s, exported? = %s",
 687                      package_entry->name()->as_C_string(),
 688                      from_module_entry->is_named() ?
 689                        from_module_entry->name()->as_C_string() : UNNAMED_MODULE,
 690                      to_module_entry->is_named() ?
 691                        to_module_entry->name()->as_C_string() : UNNAMED_MODULE,
 692                      BOOL_TO_STR(!from_module_entry->is_named() ||
 693                        package_entry->is_unqual_exported() ||
 694                        from_module_entry == to_module_entry ||
 695                        package_entry->is_qexported_to(to_module_entry)));
 696 
 697   // Return true if:
 698   // 1. from_module is unnamed because unnamed modules export all their packages (by default), or
 699   // 2. if the package is unqualifiedly exported, or
 700   // 3. if the modules are the same, or
 701   // 4. if the package is exported to to_module
 702   return (!from_module_entry->is_named() ||
 703           package_entry->is_unqual_exported() ||
 704           from_module_entry == to_module_entry ||
 705           package_entry->is_qexported_to(to_module_entry));
 706 }
 707 
 708 // This method is called by JFR and JNI.
 709 jobject Modules::get_module(jclass clazz, TRAPS) {
 710   assert(ModuleEntryTable::javabase_defined(), "Attempt to call get_module before java.base is defined");
 711 
 712   if (clazz == NULL) {
 713     THROW_MSG_(vmSymbols::java_lang_NullPointerException(),
 714                "class is null", JNI_FALSE);
 715   }
 716   oop mirror = JNIHandles::resolve_non_null(clazz);
 717   if (mirror == NULL) {
 718     log_debug(modules)("get_module(): no mirror, returning NULL");
 719     return NULL;
 720   }
 721   if (!java_lang_Class::is_instance(mirror)) {
 722     THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 723                "Invalid class", JNI_FALSE);
 724   }
 725 
 726   oop module = java_lang_Class::module(mirror);
 727 
 728   assert(module != NULL, "java.lang.Class module field not set");
 729   assert(java_lang_reflect_Module::is_instance(module), "module is not an instance of type java.lang.reflect.Module");
 730 
 731   if (log_is_enabled(Debug, modules)) {
 732     ResourceMark rm(THREAD);
 733     outputStream* logst = Log(modules)::debug_stream();
 734     Klass* klass = java_lang_Class::as_Klass(mirror);
 735     oop module_name = java_lang_reflect_Module::name(module);
 736     if (module_name != NULL) {
 737       logst->print("get_module(): module ");
 738       java_lang_String::print(module_name, tty);
 739     } else {
 740       logst->print("get_module(): Unamed Module");
 741     }
 742     if (klass != NULL) {
 743       logst->print_cr(" for class %s", klass->external_name());
 744     } else {
 745       logst->print_cr(" for primitive class");
 746     }
 747   }
 748 
 749   return JNIHandles::make_local(THREAD, module);
 750 }
 751 
 752 
 753 jobject Modules::get_module_by_package_name(jobject loader, jstring package, TRAPS) {
 754   ResourceMark rm(THREAD);
 755   assert(ModuleEntryTable::javabase_defined(),
 756          "Attempt to call get_module_from_pkg before java.base is defined");
 757 
 758   if (NULL == package) {
 759     THROW_MSG_(vmSymbols::java_lang_NullPointerException(),
 760                "package is null", JNI_FALSE);
 761   }
 762   const char* package_str =
 763     java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package));
 764   if (NULL == package_str) {
 765     THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 766                "Invalid package", JNI_FALSE);
 767   }
 768 
 769   Handle h_loader (THREAD, JNIHandles::resolve(loader));
 770   // Check that loader is a subclass of java.lang.ClassLoader.
 771   if (loader != NULL && !java_lang_ClassLoader::is_subclass(h_loader->klass())) {
 772     THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 773                "Class loader is not a subclass of java.lang.ClassLoader", JNI_FALSE);
 774   }
 775 
 776   if (strlen(package_str) == 0) {
 777     // Return the unnamed module
 778     ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK_NULL);
 779     if (NULL == module_table) return NULL;
 780     const ModuleEntry* const unnamed_module = module_table->unnamed_module();
 781     return JNIHandles::make_local(THREAD, JNIHandles::resolve(unnamed_module->module()));
 782 
 783   } else {
 784     TempNewSymbol package_sym = SymbolTable::new_symbol(package_str, CHECK_NULL);
 785     return get_module(package_sym, h_loader, CHECK_NULL);
 786   }
 787   return NULL;
 788 }
 789 
 790 
 791 jobject Modules::get_named_module(Handle h_loader, const char* package_str, TRAPS) {
 792   assert(ModuleEntryTable::javabase_defined(),
 793          "Attempt to call get_named_module before java.base is defined");
 794   assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()),
 795          "Class loader is not a subclass of java.lang.ClassLoader");
 796   assert(package_str != NULL, "the package_str should not be NULL");
 797 
 798   if (strlen(package_str) == 0) {
 799     return NULL;
 800   }
 801   TempNewSymbol package_sym = SymbolTable::new_symbol(package_str, CHECK_NULL);
 802   const PackageEntry* const pkg_entry =
 803     get_package_entry_by_name(package_sym, h_loader, THREAD);
 804   const ModuleEntry* const module_entry = (pkg_entry != NULL ? pkg_entry->module() : NULL);
 805 
 806   if (module_entry != NULL && module_entry->module() != NULL && module_entry->is_named()) {
 807     return JNIHandles::make_local(THREAD, JNIHandles::resolve(module_entry->module()));
 808   }
 809   return NULL;
 810 }
 811 
 812 
 813 // This method is called by JFR and by the above method.
 814 jobject Modules::get_module(Symbol* package_name, Handle h_loader, TRAPS) {
 815   const PackageEntry* const pkg_entry =
 816     get_package_entry_by_name(package_name, h_loader, THREAD);
 817   const ModuleEntry* const module_entry = (pkg_entry != NULL ? pkg_entry->module() : NULL);
 818 
 819   if (module_entry != NULL &&
 820       module_entry->module() != NULL) {
 821     return JNIHandles::make_local(THREAD, JNIHandles::resolve(module_entry->module()));
 822   }
 823 
 824   return NULL;
 825 }
 826 
 827 void Modules::add_module_package(jobject module, jstring package, TRAPS) {
 828   ResourceMark rm(THREAD);
 829 
 830   if (module == NULL) {
 831     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 832               "module is null");
 833   }
 834   if (package == NULL) {
 835     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 836               "package is null");
 837   }
 838   ModuleEntry* module_entry = get_module_entry(module, CHECK);
 839   if (module_entry == NULL) {
 840     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 841               "module is invalid");
 842   }
 843   if (!module_entry->is_named()) {
 844     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 845               "module cannot be an unnamed module");
 846   }
 847   char *package_name = java_lang_String::as_utf8_string(
 848     JNIHandles::resolve_non_null(package));
 849   if (package_name == NULL) {
 850     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Bad package");
 851   }
 852   if (!verify_package_name(package_name)) {
 853     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 854               err_msg("Invalid package name: %s", package_name));
 855   }
 856 
 857   log_debug(modules)("add_module_package(): Adding package %s to module %s",
 858                      package_name, module_entry->name()->as_C_string());
 859 
 860   TempNewSymbol pkg_symbol = SymbolTable::new_symbol(package_name, CHECK);
 861   PackageEntryTable* package_table = module_entry->loader_data()->packages();
 862   assert(package_table != NULL, "Missing package_table");
 863 
 864   bool pkg_exists = false;
 865   {
 866     MutexLocker ml(Module_lock, THREAD);
 867 
 868     // Check that the package does not exist in the class loader's package table.
 869     if (!package_table->lookup_only(pkg_symbol)) {
 870       PackageEntry* pkg = package_table->locked_create_entry_or_null(pkg_symbol, module_entry);
 871       assert(pkg != NULL, "Unable to create a module's package entry");
 872     } else {
 873       pkg_exists = true;
 874     }
 875   }
 876   if (pkg_exists) {
 877     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 878               err_msg("Package %s already exists for class loader", package_name));
 879   }
 880 }
 881 
 882 // Export package in module to all unnamed modules.
 883 void Modules::add_module_exports_to_all_unnamed(jobject module, jstring package, TRAPS) {
 884   if (module == NULL) {
 885     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 886               "module is null");
 887   }
 888   if (package == NULL) {
 889     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 890               "package is null");
 891   }
 892   ModuleEntry* module_entry = get_module_entry(module, CHECK);
 893   if (module_entry == NULL) {
 894     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 895               "module is invalid");
 896   }
 897 
 898   if (module_entry->is_named()) { // No-op for unnamed module.
 899     PackageEntry *package_entry = get_package_entry(module_entry, package, CHECK);
 900     ResourceMark rm(THREAD);
 901     if (package_entry == NULL) {
 902       const char *package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package));
 903       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 904                 err_msg("Package %s not found in module %s",
 905                         package_name != NULL ? package_name : "",
 906                         module_entry->name()->as_C_string()));
 907     }
 908     if (package_entry->module() != module_entry) {
 909       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 910                 err_msg("Package: %s found in module %s, not in module: %s",
 911                         package_entry->name()->as_C_string(),
 912                         package_entry->module()->name()->as_C_string(),
 913                         module_entry->name()->as_C_string()));
 914     }
 915 
 916     log_debug(modules)("add_module_exports_to_all_unnamed(): package %s in module"
 917                        " %s is exported to all unnamed modules",
 918                        package_entry->name()->as_C_string(),
 919                        module_entry->name()->as_C_string());
 920 
 921     // Mark package as exported to all unnamed modules, unless already
 922     // unqualifiedly exported.
 923     if (!package_entry->is_unqual_exported()) {
 924       package_entry->set_is_exported_allUnnamed();
 925     }
 926   }
 927 }