< prev index next >

src/share/vm/classfile/modules.cpp

Print this page
rev 12479 : 8171855: Move package name transformations during module bootstrap into native code
Reviewed-by: alanb, acorn, lfoltan, mchung


  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/systemDictionary.hpp"
  38 #include "classfile/vmSymbols.hpp"
  39 #include "logging/log.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "oops/instanceKlass.hpp"
  42 #include "oops/objArrayKlass.hpp"
  43 #include "oops/objArrayOop.inline.hpp"
  44 #include "runtime/arguments.hpp"
  45 #include "runtime/handles.inline.hpp"
  46 #include "runtime/javaCalls.hpp"
  47 #include "runtime/reflection.hpp"
  48 #include "utilities/stringUtils.hpp"
  49 #include "utilities/utf8.hpp"
  50 
  51 static bool verify_module_name(char *module_name) {
  52   if (module_name == NULL) return false;
  53   int len = (int)strlen(module_name);
  54   return (len > 0 && len <= Symbol::max_length());
  55 }
  56 
  57 bool Modules::verify_package_name(char *package_name) {
  58   if (package_name == NULL) return false;
  59   int len = (int)strlen(package_name);
  60   return (len > 0 && len <= Symbol::max_length() &&
  61     UTF8::is_legal_utf8((unsigned char *)package_name, len, false) &&
  62     ClassFileParser::verify_unqualified_name(package_name, len,
  63     ClassFileParser::LegalClass));
  64 }
  65 
  66 static char* get_module_name(oop module, TRAPS) {
  67   oop name_oop = java_lang_reflect_Module::name(module);
  68   if (name_oop == NULL) {
  69     THROW_MSG_NULL(vmSymbols::java_lang_NullPointerException(), "Null module name");
  70   }
  71   char* module_name = java_lang_String::as_utf8_string(name_oop);
  72   if (!verify_module_name(module_name)) {
  73     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
  74                    err_msg("Invalid module name: %s",
  75                            module_name != NULL ? module_name : "NULL"));
  76   }
  77   return module_name;


  90   ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader, CHECK_NULL);
  91   return loader_cld->modules();
  92 }
  93 
  94 static PackageEntryTable* get_package_entry_table(Handle h_loader, TRAPS) {
  95   // This code can be called during start-up, before the classLoader's classLoader data got
  96   // created.  So, call register_loader() to make sure the classLoader data gets created.
  97   ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader, CHECK_NULL);
  98   return loader_cld->packages();
  99 }
 100 
 101 static ModuleEntry* get_module_entry(jobject module, TRAPS) {
 102   Handle module_h(THREAD, JNIHandles::resolve(module));
 103   if (!java_lang_reflect_Module::is_instance(module_h())) {
 104     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
 105                    "module is not an instance of type java.lang.reflect.Module");
 106   }
 107   return java_lang_reflect_Module::module_entry(module_h(), CHECK_NULL);
 108 }
 109 
 110 static PackageEntry* get_package_entry(ModuleEntry* module_entry, jstring package, TRAPS) {
 111   ResourceMark rm(THREAD);
 112   if (package == NULL) return NULL;
 113   const char *package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package));
 114   if (package_name == NULL) return NULL;
 115   TempNewSymbol pkg_symbol = SymbolTable::new_symbol(package_name, CHECK_NULL);
 116   PackageEntryTable* package_entry_table = module_entry->loader_data()->packages();
 117   assert(package_entry_table != NULL, "Unexpected null package entry table");
 118   return package_entry_table->lookup_only(pkg_symbol);
 119 }
 120 
 121 static PackageEntry* get_package_entry_by_name(Symbol* package,
 122                                                Handle h_loader,
 123                                                TRAPS) {
 124   if (package != NULL) {
 125     ResourceMark rm(THREAD);
 126     if (Modules::verify_package_name(package->as_C_string())) {
 127       PackageEntryTable* const package_entry_table =
 128         get_package_entry_table(h_loader, CHECK_NULL);
 129       assert(package_entry_table != NULL, "Unexpected null package entry table");
 130       return package_entry_table->lookup_only(package);
 131     }
 132   }
 133   return NULL;
 134 }
 135 
 136 bool Modules::is_package_defined(Symbol* package, Handle h_loader, TRAPS) {
 137   PackageEntry* res = get_package_entry_by_name(package, h_loader, CHECK_false);
 138   return res != NULL;
 139 }
 140 
 141 static void define_javabase_module(jobject module, jstring version,
 142                                    jstring location, jobjectArray 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   objArrayOop packages_oop = objArrayOop(JNIHandles::resolve(packages));
 168   objArrayHandle packages_h(THREAD, packages_oop);
 169   int num_packages = (packages_h == NULL ? 0 : packages_h->length());
 170 
 171   // Check that the list of packages has no duplicates and that the
 172   // packages are syntactically ok.
 173   GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages);
 174   for (int x = 0; x < num_packages; x++) {
 175     oop string_obj = packages_h->obj_at(x);
 176 
 177     if (string_obj == NULL || !string_obj->is_a(SystemDictionary::String_klass())) {
 178       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 179                 "Bad package name for module: " JAVA_BASE_NAME);
 180     }
 181     char *package_name = java_lang_String::as_utf8_string(string_obj);
 182     if (!Modules::verify_package_name(package_name)) {
 183       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 184                 err_msg("Invalid package name: %s for module: " JAVA_BASE_NAME, package_name));
 185     }
 186     Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, CHECK);
 187     // append_if_missing() returns FALSE if entry already exists.
 188     if (!pkg_list->append_if_missing(pkg_symbol)) {
 189       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 190                 err_msg("Duplicate package name: %s for module " JAVA_BASE_NAME,
 191                         package_name));
 192     }
 193   }
 194 
 195   // Validate java_base's loader is the boot loader.
 196   oop loader = java_lang_reflect_Module::loader(module_handle());
 197   if (loader != NULL) {
 198     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 199               "Class loader must be the boot class loader");
 200   }
 201   Handle h_loader = Handle(THREAD, loader);


 246   // Only the thread that actually defined the base module will get here,
 247   // so no locking is needed.
 248 
 249   // Patch any previously loaded class's module field with java.base's java.lang.reflect.Module.
 250   ModuleEntryTable::patch_javabase_entries(module_handle);
 251 
 252   log_debug(modules)("define_javabase_module(): Definition of module: "
 253                      JAVA_BASE_NAME ", version: %s, location: %s, package #: %d",
 254                      module_version != NULL ? module_version : "NULL",
 255                      module_location != NULL ? module_location : "NULL",
 256                      pkg_list->length());
 257 
 258   // packages defined to java.base
 259   for (int x = 0; x < pkg_list->length(); x++) {
 260     log_trace(modules)("define_javabase_module(): creation of package %s for module " JAVA_BASE_NAME,
 261                        (pkg_list->at(x))->as_C_string());
 262   }
 263 }
 264 
 265 void Modules::define_module(jobject module, jstring version,
 266                             jstring location, jobjectArray packages, TRAPS) {

 267   ResourceMark rm(THREAD);
 268 
 269   if (module == NULL) {
 270     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
 271   }
 272   Handle module_handle(THREAD, JNIHandles::resolve(module));
 273   if (!java_lang_reflect_Module::is_instance(module_handle())) {
 274     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 275               "module is not an instance of type java.lang.reflect.Module");
 276   }
 277 
 278   char* module_name = get_module_name(module_handle(), CHECK);
 279   if (module_name == NULL) {
 280     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 281               "Module name cannot be null");
 282   }
 283 
 284   // Special handling of java.base definition
 285   if (strcmp(module_name, JAVA_BASE_NAME) == 0) {
 286     define_javabase_module(module, version, location, packages, CHECK);
 287     return;
 288   }
 289 
 290   const char* module_version = get_module_version(version);
 291 
 292   oop loader = java_lang_reflect_Module::loader(module_handle());
 293   // Make sure loader is not the jdk.internal.reflect.DelegatingClassLoader.
 294   if (loader != java_lang_ClassLoader::non_reflection_class_loader(loader)) {
 295     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 296               "Class loader is an invalid delegating class loader");
 297   }
 298   Handle h_loader = Handle(THREAD, loader);
 299 
 300   objArrayOop packages_oop = objArrayOop(JNIHandles::resolve(packages));
 301   objArrayHandle packages_h(THREAD, packages_oop);
 302   int num_packages = (packages_h == NULL ? 0 : packages_h->length());
 303 
 304   // Check that the list of packages has no duplicates and that the
 305   // packages are syntactically ok.
 306   GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages);
 307   for (int x = 0; x < num_packages; x++) {
 308     oop string_obj = packages_h->obj_at(x);
 309 
 310     if (string_obj == NULL || !string_obj->is_a(SystemDictionary::String_klass())) {
 311       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 312                 err_msg("Bad package name for module: %s", module_name));
 313     }
 314     char *package_name = java_lang_String::as_utf8_string(string_obj);
 315     if (!verify_package_name(package_name)) {
 316       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 317                 err_msg("Invalid package name: %s for module: %s",
 318                         package_name, module_name));
 319     }
 320 
 321     // Only modules defined to either the boot or platform class loader, can define a "java/" package.
 322     if (!h_loader.is_null() &&
 323         !SystemDictionary::is_platform_class_loader(h_loader) &&
 324         strncmp(package_name, JAVAPKG, JAVAPKG_LEN) == 0) {
 325       const char* class_loader_name = SystemDictionary::loader_name(h_loader());
 326       StringUtils::replace_no_expand(package_name, "/", ".");


 327       const char* msg_text1 = "Class loader (instance of): ";
 328       const char* msg_text2 = " tried to define prohibited package name: ";
 329       size_t len = strlen(msg_text1) + strlen(class_loader_name) + strlen(msg_text2) + strlen(package_name) + 1;
 330       char* message = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len);
 331       jio_snprintf(message, len, "%s%s%s%s", msg_text1, class_loader_name, msg_text2, package_name);
 332       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), message);
 333     }
 334 
 335     Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, CHECK);
 336     // append_if_missing() returns FALSE if entry already exists.
 337     if (!pkg_list->append_if_missing(pkg_symbol)) {
 338       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 339                 err_msg("Duplicate package name: %s for module %s",
 340                         package_name, module_name));
 341     }
 342   }
 343 
 344   ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK);
 345   assert(module_table != NULL, "module entry table shouldn't be null");
 346 
 347   // Create symbol* entry for module name.
 348   TempNewSymbol module_symbol = SymbolTable::new_symbol(module_name, CHECK);
 349 
 350   int dupl_pkg_index = -1;
 351   bool dupl_modules = false;


 480   oop loader = java_lang_reflect_Module::loader(module_handle());
 481   if (loader != NULL) {
 482     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 483               "Class loader must be the boot class loader");
 484   }
 485   Handle h_loader = Handle(THREAD, loader);
 486 
 487   log_debug(modules)("set_bootloader_unnamed_module(): recording unnamed module for boot loader");
 488 
 489   // Ensure the boot loader's PackageEntryTable has been created
 490   ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK);
 491 
 492   // Set java.lang.reflect.Module for the boot loader's unnamed module
 493   ModuleEntry* unnamed_module = module_table->unnamed_module();
 494   assert(unnamed_module != NULL, "boot loader's unnamed ModuleEntry not defined");
 495   unnamed_module->set_module(ClassLoaderData::the_null_class_loader_data()->add_handle(module_handle));
 496   // Store pointer to the ModuleEntry in the unnamed module's java.lang.reflect.Module object.
 497   java_lang_reflect_Module::set_module_entry(module_handle(), unnamed_module);
 498 }
 499 
 500 void Modules::add_module_exports(jobject from_module, jstring package, jobject to_module, TRAPS) {
 501   if (package == NULL) {
 502     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 503               "package is null");
 504   }
 505   if (from_module == NULL) {
 506     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 507               "from_module is null");
 508   }
 509   ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
 510   if (from_module_entry == NULL) {
 511     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 512               "from_module cannot be found");
 513   }
 514 
 515   // All packages in unnamed are exported by default.
 516   if (!from_module_entry->is_named()) return;
 517 
 518   ModuleEntry* to_module_entry;
 519   if (to_module == NULL) {
 520     to_module_entry = NULL;  // It's an unqualified export.
 521   } else {
 522     to_module_entry = get_module_entry(to_module, CHECK);
 523     if (to_module_entry == NULL) {
 524       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 525                 "to_module is invalid");
 526     }
 527   }
 528 
 529   PackageEntry *package_entry = get_package_entry(from_module_entry, package, CHECK);
 530   ResourceMark rm(THREAD);
 531   if (package_entry == NULL) {
 532     const char *package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package));
 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(modules)("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, jstring 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 


 632     outputStream* logst = Log(modules)::debug_stream();
 633     Klass* klass = java_lang_Class::as_Klass(mirror);
 634     oop module_name = java_lang_reflect_Module::name(module);
 635     if (module_name != NULL) {
 636       logst->print("get_module(): module ");
 637       java_lang_String::print(module_name, tty);
 638     } else {
 639       logst->print("get_module(): Unamed Module");
 640     }
 641     if (klass != NULL) {
 642       logst->print_cr(" for class %s", klass->external_name());
 643     } else {
 644       logst->print_cr(" for primitive class");
 645     }
 646   }
 647 
 648   return JNIHandles::make_local(THREAD, module);
 649 }
 650 
 651 
 652 jobject Modules::get_module_by_package_name(jobject loader, jstring package, TRAPS) {
 653   ResourceMark rm(THREAD);
 654   assert(ModuleEntryTable::javabase_defined(),
 655          "Attempt to call get_module_from_pkg before " JAVA_BASE_NAME " is defined");
 656 
 657   if (NULL == package) {
 658     THROW_MSG_(vmSymbols::java_lang_NullPointerException(),
 659                "package is null", JNI_FALSE);
 660   }
 661   const char* package_str =
 662     java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package));
 663   if (NULL == package_str) {
 664     THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 665                "Invalid package", JNI_FALSE);
 666   }
 667 
 668   Handle h_loader (THREAD, JNIHandles::resolve(loader));
 669   // Check that loader is a subclass of java.lang.ClassLoader.
 670   if (loader != NULL && !java_lang_ClassLoader::is_subclass(h_loader->klass())) {
 671     THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 672                "Class loader is not a subclass of java.lang.ClassLoader", JNI_FALSE);
 673   }
 674 
 675   if (strlen(package_str) == 0) {
 676     // Return the unnamed module
 677     ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK_NULL);
 678     if (NULL == module_table) return NULL;
 679     const ModuleEntry* const unnamed_module = module_table->unnamed_module();
 680     return JNIHandles::make_local(THREAD, JNIHandles::resolve(unnamed_module->module()));
 681 
 682   } else {
 683     TempNewSymbol package_sym = SymbolTable::new_symbol(package_str, CHECK_NULL);
 684     return get_module(package_sym, h_loader, CHECK_NULL);
 685   }
 686   return NULL;
 687 }
 688 
 689 
 690 jobject Modules::get_named_module(Handle h_loader, const char* package_str, TRAPS) {
 691   assert(ModuleEntryTable::javabase_defined(),
 692          "Attempt to call get_named_module before " JAVA_BASE_NAME " is defined");
 693   assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()),
 694          "Class loader is not a subclass of java.lang.ClassLoader");
 695   assert(package_str != NULL, "the package_str should not be NULL");
 696 
 697   if (strlen(package_str) == 0) {
 698     return NULL;
 699   }
 700   TempNewSymbol package_sym = SymbolTable::new_symbol(package_str, CHECK_NULL);
 701   const PackageEntry* const pkg_entry =
 702     get_package_entry_by_name(package_sym, h_loader, THREAD);
 703   const ModuleEntry* const module_entry = (pkg_entry != NULL ? pkg_entry->module() : NULL);
 704 
 705   if (module_entry != NULL && module_entry->module() != NULL && module_entry->is_named()) {
 706     return JNIHandles::make_local(THREAD, JNIHandles::resolve(module_entry->module()));
 707   }
 708   return NULL;
 709 }
 710 
 711 
 712 // This method is called by JFR and by the above method.
 713 jobject Modules::get_module(Symbol* package_name, Handle h_loader, TRAPS) {
 714   const PackageEntry* const pkg_entry =
 715     get_package_entry_by_name(package_name, h_loader, THREAD);
 716   const ModuleEntry* const module_entry = (pkg_entry != NULL ? pkg_entry->module() : NULL);
 717 
 718   if (module_entry != NULL &&
 719       module_entry->module() != NULL) {
 720     return JNIHandles::make_local(THREAD, JNIHandles::resolve(module_entry->module()));
 721   }
 722 
 723   return NULL;
 724 }
 725 
 726 void Modules::add_module_package(jobject module, jstring package, TRAPS) {
 727   ResourceMark rm(THREAD);
 728 
 729   if (module == NULL) {
 730     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 731               "module is null");
 732   }
 733   if (package == NULL) {
 734     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 735               "package is null");
 736   }
 737   ModuleEntry* module_entry = get_module_entry(module, CHECK);
 738   if (module_entry == NULL) {
 739     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 740               "module is invalid");
 741   }
 742   if (!module_entry->is_named()) {
 743     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 744               "module cannot be an unnamed module");
 745   }
 746   char *package_name = java_lang_String::as_utf8_string(
 747     JNIHandles::resolve_non_null(package));
 748   if (package_name == NULL) {
 749     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Bad package");
 750   }
 751   if (!verify_package_name(package_name)) {
 752     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 753               err_msg("Invalid package name: %s", package_name));
 754   }
 755 
 756   ClassLoaderData *loader_data = module_entry->loader_data();
 757 
 758   // Only modules defined to either the boot or platform class loader, can define a "java/" package.
 759   if (!loader_data->is_the_null_class_loader_data() &&
 760       !loader_data->is_platform_class_loader_data() &&
 761       strncmp(package_name, JAVAPKG, JAVAPKG_LEN) == 0) {
 762     const char* class_loader_name = SystemDictionary::loader_name(loader_data);
 763     StringUtils::replace_no_expand(package_name, "/", ".");


 764     const char* msg_text1 = "Class loader (instance of): ";
 765     const char* msg_text2 = " tried to define prohibited package name: ";
 766     size_t len = strlen(msg_text1) + strlen(class_loader_name) + strlen(msg_text2) + strlen(package_name) + 1;
 767     char* message = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len);
 768     jio_snprintf(message, len, "%s%s%s%s", msg_text1, class_loader_name, msg_text2, package_name);
 769     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), message);
 770   }
 771 
 772   log_debug(modules)("add_module_package(): Adding package %s to module %s",
 773                      package_name, module_entry->name()->as_C_string());
 774 
 775   TempNewSymbol pkg_symbol = SymbolTable::new_symbol(package_name, CHECK);
 776   PackageEntryTable* package_table = loader_data->packages();
 777   assert(package_table != NULL, "Missing package_table");
 778 
 779   bool pkg_exists = false;
 780   {
 781     MutexLocker ml(Module_lock, THREAD);
 782 
 783     // Check that the package does not exist in the class loader's package table.
 784     if (!package_table->lookup_only(pkg_symbol)) {
 785       PackageEntry* pkg = package_table->locked_create_entry_or_null(pkg_symbol, module_entry);
 786       assert(pkg != NULL, "Unable to create a module's package entry");
 787     } else {
 788       pkg_exists = true;
 789     }
 790   }
 791   if (pkg_exists) {
 792     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 793               err_msg("Package %s already exists for class loader", package_name));
 794   }
 795 }
 796 
 797 // Export package in module to all unnamed modules.
 798 void Modules::add_module_exports_to_all_unnamed(jobject module, jstring package, TRAPS) {
 799   if (module == NULL) {
 800     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 801               "module is null");
 802   }
 803   if (package == NULL) {
 804     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 805               "package is null");
 806   }
 807   ModuleEntry* module_entry = get_module_entry(module, CHECK);
 808   if (module_entry == NULL) {
 809     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 810               "module is invalid");
 811   }
 812 
 813   if (module_entry->is_named()) { // No-op for unnamed module.
 814     PackageEntry *package_entry = get_package_entry(module_entry, package, CHECK);
 815     ResourceMark rm(THREAD);
 816     if (package_entry == NULL) {
 817       const char *package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package));
 818       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 819                 err_msg("Package %s not found in module %s",
 820                         package_name != NULL ? package_name : "",
 821                         module_entry->name()->as_C_string()));
 822     }
 823     if (package_entry->module() != module_entry) {
 824       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 825                 err_msg("Package: %s found in module %s, not in module: %s",
 826                         package_entry->name()->as_C_string(),
 827                         package_entry->module()->name()->as_C_string(),
 828                         module_entry->name()->as_C_string()));
 829     }
 830 
 831     log_debug(modules)("add_module_exports_to_all_unnamed(): package %s in module"
 832                        " %s is exported to all unnamed modules",
 833                        package_entry->name()->as_C_string(),
 834                        module_entry->name()->as_C_string());
 835 
 836     // Mark package as exported to all unnamed modules, unless already
 837     // unqualifiedly exported.


  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/systemDictionary.hpp"
  38 #include "classfile/vmSymbols.hpp"
  39 #include "logging/log.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "oops/instanceKlass.hpp"


  42 #include "runtime/arguments.hpp"
  43 #include "runtime/handles.inline.hpp"
  44 #include "runtime/javaCalls.hpp"
  45 #include "runtime/reflection.hpp"
  46 #include "utilities/stringUtils.hpp"
  47 #include "utilities/utf8.hpp"
  48 
  49 static bool verify_module_name(const 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 }
  54 
  55 bool Modules::verify_package_name(const char* package_name) {
  56   if (package_name == NULL) return false;
  57   int len = (int)strlen(package_name);
  58   return (len > 0 && len <= Symbol::max_length() &&
  59     UTF8::is_legal_utf8((unsigned char *)package_name, len, false) &&
  60     ClassFileParser::verify_unqualified_name(package_name, len,
  61     ClassFileParser::LegalClass));
  62 }
  63 
  64 static char* get_module_name(oop module, TRAPS) {
  65   oop name_oop = java_lang_reflect_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);
  70   if (!verify_module_name(module_name)) {
  71     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
  72                    err_msg("Invalid module name: %s",
  73                            module_name != NULL ? module_name : "NULL"));
  74   }
  75   return module_name;


  88   ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader, CHECK_NULL);
  89   return loader_cld->modules();
  90 }
  91 
  92 static PackageEntryTable* get_package_entry_table(Handle h_loader, TRAPS) {
  93   // This code can be called during start-up, before the classLoader's classLoader data got
  94   // created.  So, call register_loader() to make sure the classLoader data gets created.
  95   ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader, CHECK_NULL);
  96   return loader_cld->packages();
  97 }
  98 
  99 static ModuleEntry* get_module_entry(jobject module, TRAPS) {
 100   Handle module_h(THREAD, JNIHandles::resolve(module));
 101   if (!java_lang_reflect_Module::is_instance(module_h())) {
 102     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
 103                    "module is not an instance of type java.lang.reflect.Module");
 104   }
 105   return java_lang_reflect_Module::module_entry(module_h(), CHECK_NULL);
 106 }
 107 
 108 static PackageEntry* get_package_entry(ModuleEntry* module_entry, const char* package_name, TRAPS) {
 109   ResourceMark rm(THREAD);


 110   if (package_name == NULL) return NULL;
 111   TempNewSymbol pkg_symbol = SymbolTable::new_symbol(package_name, CHECK_NULL);
 112   PackageEntryTable* package_entry_table = module_entry->loader_data()->packages();
 113   assert(package_entry_table != NULL, "Unexpected null package entry table");
 114   return package_entry_table->lookup_only(pkg_symbol);
 115 }
 116 
 117 static PackageEntry* get_package_entry_by_name(Symbol* package,
 118                                                Handle h_loader,
 119                                                TRAPS) {
 120   if (package != NULL) {
 121     ResourceMark rm(THREAD);
 122     if (Modules::verify_package_name(package->as_C_string())) {
 123       PackageEntryTable* const package_entry_table =
 124         get_package_entry_table(h_loader, CHECK_NULL);
 125       assert(package_entry_table != NULL, "Unexpected null package entry table");
 126       return package_entry_table->lookup_only(package);
 127     }
 128   }
 129   return NULL;
 130 }
 131 
 132 bool Modules::is_package_defined(Symbol* package, Handle h_loader, TRAPS) {
 133   PackageEntry* res = get_package_entry_by_name(package, h_loader, CHECK_false);
 134   return res != NULL;
 135 }
 136 
 137 static void define_javabase_module(jobject module, jstring version,
 138                                    jstring location, const char* const* packages,
 139                                    jsize num_packages, TRAPS) {
 140   ResourceMark rm(THREAD);
 141 
 142   Handle module_handle(THREAD, JNIHandles::resolve(module));
 143 
 144   // Obtain java.base's module version
 145   const char* module_version = get_module_version(version);
 146   TempNewSymbol version_symbol;
 147   if (module_version != NULL) {
 148     version_symbol = SymbolTable::new_symbol(module_version, CHECK);
 149   } else {
 150     version_symbol = NULL;
 151   }
 152 
 153   // Obtain java.base's location
 154   const char* module_location = NULL;
 155   TempNewSymbol location_symbol = NULL;
 156   if (location != NULL) {
 157     module_location =
 158       java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(location));
 159     if (module_location != NULL) {
 160       location_symbol = SymbolTable::new_symbol(module_location, CHECK);
 161     }
 162   }
 163 



 164 
 165   // Check that the list of packages has no duplicates and that the
 166   // packages are syntactically ok.
 167   GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages);
 168   for (int x = 0; x < num_packages; x++) {
 169     const char *package_name = packages[x];






 170     if (!Modules::verify_package_name(package_name)) {
 171       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 172                 err_msg("Invalid package name: %s for module: " JAVA_BASE_NAME, package_name));
 173     }
 174     Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, CHECK);
 175     // append_if_missing() returns FALSE if entry already exists.
 176     if (!pkg_list->append_if_missing(pkg_symbol)) {
 177       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 178                 err_msg("Duplicate package name: %s for module " JAVA_BASE_NAME,
 179                         package_name));
 180     }
 181   }
 182 
 183   // Validate java_base's loader is the boot loader.
 184   oop loader = java_lang_reflect_Module::loader(module_handle());
 185   if (loader != NULL) {
 186     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 187               "Class loader must be the boot class loader");
 188   }
 189   Handle h_loader = Handle(THREAD, loader);


 234   // Only the thread that actually defined the base module will get here,
 235   // so no locking is needed.
 236 
 237   // Patch any previously loaded class's module field with java.base's java.lang.reflect.Module.
 238   ModuleEntryTable::patch_javabase_entries(module_handle);
 239 
 240   log_debug(modules)("define_javabase_module(): Definition of module: "
 241                      JAVA_BASE_NAME ", version: %s, location: %s, package #: %d",
 242                      module_version != NULL ? module_version : "NULL",
 243                      module_location != NULL ? module_location : "NULL",
 244                      pkg_list->length());
 245 
 246   // packages defined to java.base
 247   for (int x = 0; x < pkg_list->length(); x++) {
 248     log_trace(modules)("define_javabase_module(): creation of package %s for module " JAVA_BASE_NAME,
 249                        (pkg_list->at(x))->as_C_string());
 250   }
 251 }
 252 
 253 void Modules::define_module(jobject module, jstring version,
 254                             jstring location, const char* const* packages,
 255                             jsize num_packages, TRAPS) {
 256   ResourceMark rm(THREAD);
 257 
 258   if (module == NULL) {
 259     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
 260   }
 261   Handle module_handle(THREAD, JNIHandles::resolve(module));
 262   if (!java_lang_reflect_Module::is_instance(module_handle())) {
 263     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 264               "module is not an instance of type java.lang.reflect.Module");
 265   }
 266 
 267   char* module_name = get_module_name(module_handle(), CHECK);
 268   if (module_name == NULL) {
 269     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 270               "Module name cannot be null");
 271   }
 272 
 273   // Special handling of java.base definition
 274   if (strcmp(module_name, JAVA_BASE_NAME) == 0) {
 275     define_javabase_module(module, version, location, packages, num_packages, CHECK);
 276     return;
 277   }
 278 
 279   const char* module_version = get_module_version(version);
 280 
 281   oop loader = java_lang_reflect_Module::loader(module_handle());
 282   // Make sure loader is not the jdk.internal.reflect.DelegatingClassLoader.
 283   if (loader != java_lang_ClassLoader::non_reflection_class_loader(loader)) {
 284     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 285               "Class loader is an invalid delegating class loader");
 286   }
 287   Handle h_loader = Handle(THREAD, loader);
 288 




 289   // Check that the list of packages has no duplicates and that the
 290   // packages are syntactically ok.
 291   GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages);
 292   for (int x = 0; x < num_packages; x++) {
 293     const char* package_name = packages[x];






 294     if (!verify_package_name(package_name)) {
 295       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 296                 err_msg("Invalid package name: %s for module: %s",
 297                         package_name, module_name));
 298     }
 299 
 300     // Only modules defined to either the boot or platform class loader, can define a "java/" package.
 301     if (!h_loader.is_null() &&
 302         !SystemDictionary::is_platform_class_loader(h_loader) &&
 303         strncmp(package_name, JAVAPKG, JAVAPKG_LEN) == 0) {
 304       const char* class_loader_name = SystemDictionary::loader_name(h_loader());
 305       char* pkg_name = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(package_name));
 306       strcpy(pkg_name, package_name);
 307       StringUtils::replace_no_expand(pkg_name, "/", ".");
 308       const char* msg_text1 = "Class loader (instance of): ";
 309       const char* msg_text2 = " tried to define prohibited package name: ";
 310       size_t len = strlen(msg_text1) + strlen(class_loader_name) + strlen(msg_text2) + strlen(pkg_name) + 1;
 311       char* message = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len);
 312       jio_snprintf(message, len, "%s%s%s%s", msg_text1, class_loader_name, msg_text2, pkg_name);
 313       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), message);
 314     }
 315 
 316     Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, CHECK);
 317     // append_if_missing() returns FALSE if entry already exists.
 318     if (!pkg_list->append_if_missing(pkg_symbol)) {
 319       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 320                 err_msg("Duplicate package name: %s for module %s",
 321                         package_name, module_name));
 322     }
 323   }
 324 
 325   ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK);
 326   assert(module_table != NULL, "module entry table shouldn't be null");
 327 
 328   // Create symbol* entry for module name.
 329   TempNewSymbol module_symbol = SymbolTable::new_symbol(module_name, CHECK);
 330 
 331   int dupl_pkg_index = -1;
 332   bool dupl_modules = false;


 461   oop loader = java_lang_reflect_Module::loader(module_handle());
 462   if (loader != NULL) {
 463     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 464               "Class loader must be the boot class loader");
 465   }
 466   Handle h_loader = Handle(THREAD, loader);
 467 
 468   log_debug(modules)("set_bootloader_unnamed_module(): recording unnamed module for boot loader");
 469 
 470   // Ensure the boot loader's PackageEntryTable has been created
 471   ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK);
 472 
 473   // Set java.lang.reflect.Module for the boot loader's unnamed module
 474   ModuleEntry* unnamed_module = module_table->unnamed_module();
 475   assert(unnamed_module != NULL, "boot loader's unnamed ModuleEntry not defined");
 476   unnamed_module->set_module(ClassLoaderData::the_null_class_loader_data()->add_handle(module_handle));
 477   // Store pointer to the ModuleEntry in the unnamed module's java.lang.reflect.Module object.
 478   java_lang_reflect_Module::set_module_entry(module_handle(), unnamed_module);
 479 }
 480 
 481 void Modules::add_module_exports(jobject from_module, const char* package_name, jobject to_module, TRAPS) {
 482   if (package_name == NULL) {
 483     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 484               "package is null");
 485   }
 486   if (from_module == NULL) {
 487     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 488               "from_module is null");
 489   }
 490   ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
 491   if (from_module_entry == NULL) {
 492     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 493               "from_module cannot be found");
 494   }
 495 
 496   // All packages in unnamed are exported by default.
 497   if (!from_module_entry->is_named()) return;
 498 
 499   ModuleEntry* to_module_entry;
 500   if (to_module == NULL) {
 501     to_module_entry = NULL;  // It's an unqualified export.
 502   } else {
 503     to_module_entry = get_module_entry(to_module, CHECK);
 504     if (to_module_entry == NULL) {
 505       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 506                 "to_module is invalid");
 507     }
 508   }
 509 
 510   PackageEntry *package_entry = get_package_entry(from_module_entry, package_name, CHECK);
 511   ResourceMark rm(THREAD);
 512   if (package_entry == NULL) {

 513     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 514               err_msg("Package %s not found in from_module %s",
 515                       package_name != NULL ? package_name : "",
 516                       from_module_entry->name()->as_C_string()));
 517   }
 518   if (package_entry->module() != from_module_entry) {
 519     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 520               err_msg("Package: %s found in module %s, not in from_module: %s",
 521                       package_entry->name()->as_C_string(),
 522                       package_entry->module()->name()->as_C_string(),
 523                       from_module_entry->name()->as_C_string()));
 524   }
 525 
 526   log_debug(modules)("add_module_exports(): package %s in module %s is exported to module %s",
 527                      package_entry->name()->as_C_string(),
 528                      from_module_entry->name()->as_C_string(),
 529                      to_module_entry == NULL ? "NULL" :
 530                       to_module_entry->is_named() ?
 531                         to_module_entry->name()->as_C_string() : UNNAMED_MODULE);
 532 
 533   // Do nothing if modules are the same.
 534   if (from_module_entry != to_module_entry) {
 535     package_entry->set_exported(to_module_entry);
 536   }
 537 }
 538 
 539 
 540 void Modules::add_module_exports_qualified(jobject from_module, const char* package,
 541                                            jobject to_module, TRAPS) {
 542   if (to_module == NULL) {
 543     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 544               "to_module is null");
 545   }
 546   add_module_exports(from_module, package, to_module, CHECK);
 547 }
 548 
 549 void Modules::add_reads_module(jobject from_module, jobject to_module, TRAPS) {
 550   if (from_module == NULL) {
 551     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 552               "from_module is null");
 553   }
 554 
 555   ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
 556   if (from_module_entry == NULL) {
 557     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 558               "from_module is not valid");
 559   }
 560 


 612     outputStream* logst = Log(modules)::debug_stream();
 613     Klass* klass = java_lang_Class::as_Klass(mirror);
 614     oop module_name = java_lang_reflect_Module::name(module);
 615     if (module_name != NULL) {
 616       logst->print("get_module(): module ");
 617       java_lang_String::print(module_name, tty);
 618     } else {
 619       logst->print("get_module(): Unamed Module");
 620     }
 621     if (klass != NULL) {
 622       logst->print_cr(" for class %s", klass->external_name());
 623     } else {
 624       logst->print_cr(" for primitive class");
 625     }
 626   }
 627 
 628   return JNIHandles::make_local(THREAD, module);
 629 }
 630 
 631 
 632 jobject Modules::get_module_by_package_name(jobject loader, const char* package_name, TRAPS) {
 633   ResourceMark rm(THREAD);
 634   assert(ModuleEntryTable::javabase_defined(),
 635          "Attempt to call get_module_from_pkg before " JAVA_BASE_NAME " is defined");
 636 
 637   if (package_name == NULL) {
 638     THROW_MSG_(vmSymbols::java_lang_NullPointerException(),
 639                "package is null", JNI_FALSE);
 640   }






 641 
 642   Handle h_loader (THREAD, JNIHandles::resolve(loader));
 643   // Check that loader is a subclass of java.lang.ClassLoader.
 644   if (loader != NULL && !java_lang_ClassLoader::is_subclass(h_loader->klass())) {
 645     THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 646                "Class loader is not a subclass of java.lang.ClassLoader", JNI_FALSE);
 647   }
 648 
 649   if (strlen(package_name) == 0) {
 650     // Return the unnamed module
 651     ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK_NULL);
 652     if (NULL == module_table) return NULL;
 653     const ModuleEntry* const unnamed_module = module_table->unnamed_module();
 654     return JNIHandles::make_local(THREAD, JNIHandles::resolve(unnamed_module->module()));
 655 
 656   } else {
 657     TempNewSymbol package_sym = SymbolTable::new_symbol(package_name, CHECK_NULL);
 658     return get_module(package_sym, h_loader, CHECK_NULL);
 659   }
 660   return NULL;
 661 }
 662 
 663 
 664 jobject Modules::get_named_module(Handle h_loader, const char* package_name, TRAPS) {
 665   assert(ModuleEntryTable::javabase_defined(),
 666          "Attempt to call get_named_module before " JAVA_BASE_NAME " is defined");
 667   assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()),
 668          "Class loader is not a subclass of java.lang.ClassLoader");
 669   assert(package_name != NULL, "the package_name should not be NULL");
 670 
 671   if (strlen(package_name) == 0) {
 672     return NULL;
 673   }
 674   TempNewSymbol package_sym = SymbolTable::new_symbol(package_name, CHECK_NULL);
 675   const PackageEntry* const pkg_entry =
 676     get_package_entry_by_name(package_sym, h_loader, THREAD);
 677   const ModuleEntry* const module_entry = (pkg_entry != NULL ? pkg_entry->module() : NULL);
 678 
 679   if (module_entry != NULL && module_entry->module() != NULL && module_entry->is_named()) {
 680     return JNIHandles::make_local(THREAD, JNIHandles::resolve(module_entry->module()));
 681   }
 682   return NULL;
 683 }
 684 
 685 
 686 // This method is called by JFR and by the above method.
 687 jobject Modules::get_module(Symbol* package_name, Handle h_loader, TRAPS) {
 688   const PackageEntry* const pkg_entry =
 689     get_package_entry_by_name(package_name, h_loader, THREAD);
 690   const ModuleEntry* const module_entry = (pkg_entry != NULL ? pkg_entry->module() : NULL);
 691 
 692   if (module_entry != NULL &&
 693       module_entry->module() != NULL) {
 694     return JNIHandles::make_local(THREAD, JNIHandles::resolve(module_entry->module()));
 695   }
 696 
 697   return NULL;
 698 }
 699 
 700 void Modules::add_module_package(jobject module, const char* package_name, TRAPS) {
 701   ResourceMark rm(THREAD);
 702 
 703   if (module == NULL) {
 704     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 705               "module is null");
 706   }
 707   if (package_name == NULL) {
 708     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 709               "package is null");
 710   }
 711   ModuleEntry* module_entry = get_module_entry(module, CHECK);
 712   if (module_entry == NULL) {
 713     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 714               "module is invalid");
 715   }
 716   if (!module_entry->is_named()) {
 717     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 718               "module cannot be an unnamed module");
 719   }





 720   if (!verify_package_name(package_name)) {
 721     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 722               err_msg("Invalid package name: %s", package_name));
 723   }
 724 
 725   ClassLoaderData *loader_data = module_entry->loader_data();
 726 
 727   // Only modules defined to either the boot or platform class loader, can define a "java/" package.
 728   if (!loader_data->is_the_null_class_loader_data() &&
 729       !loader_data->is_platform_class_loader_data() &&
 730       strncmp(package_name, JAVAPKG, JAVAPKG_LEN) == 0) {
 731     const char* class_loader_name = SystemDictionary::loader_name(loader_data);
 732     char* pkg_name = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(package_name));
 733     strcpy(pkg_name, package_name);
 734     StringUtils::replace_no_expand(pkg_name, "/", ".");
 735     const char* msg_text1 = "Class loader (instance of): ";
 736     const char* msg_text2 = " tried to define prohibited package name: ";
 737     size_t len = strlen(msg_text1) + strlen(class_loader_name) + strlen(msg_text2) + strlen(pkg_name) + 1;
 738     char* message = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, len);
 739     jio_snprintf(message, len, "%s%s%s%s", msg_text1, class_loader_name, msg_text2, pkg_name);
 740     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), message);
 741   }
 742 
 743   log_debug(modules)("add_module_package(): Adding package %s to module %s",
 744                      package_name, module_entry->name()->as_C_string());
 745 
 746   TempNewSymbol pkg_symbol = SymbolTable::new_symbol(package_name, CHECK);
 747   PackageEntryTable* package_table = loader_data->packages();
 748   assert(package_table != NULL, "Missing package_table");
 749 
 750   bool pkg_exists = false;
 751   {
 752     MutexLocker ml(Module_lock, THREAD);
 753 
 754     // Check that the package does not exist in the class loader's package table.
 755     if (!package_table->lookup_only(pkg_symbol)) {
 756       PackageEntry* pkg = package_table->locked_create_entry_or_null(pkg_symbol, module_entry);
 757       assert(pkg != NULL, "Unable to create a module's package entry");
 758     } else {
 759       pkg_exists = true;
 760     }
 761   }
 762   if (pkg_exists) {
 763     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 764               err_msg("Package %s already exists for class loader", package_name));
 765   }
 766 }
 767 
 768 // Export package in module to all unnamed modules.
 769 void Modules::add_module_exports_to_all_unnamed(jobject module, const char* package_name, TRAPS) {
 770   if (module == NULL) {
 771     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 772               "module is null");
 773   }
 774   if (package_name == NULL) {
 775     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
 776               "package is null");
 777   }
 778   ModuleEntry* module_entry = get_module_entry(module, CHECK);
 779   if (module_entry == NULL) {
 780     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 781               "module is invalid");
 782   }
 783 
 784   if (module_entry->is_named()) { // No-op for unnamed module.
 785     PackageEntry *package_entry = get_package_entry(module_entry, package_name, CHECK);
 786     ResourceMark rm(THREAD);
 787     if (package_entry == NULL) {

 788       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 789                 err_msg("Package %s not found in module %s",
 790                         package_name != NULL ? package_name : "",
 791                         module_entry->name()->as_C_string()));
 792     }
 793     if (package_entry->module() != module_entry) {
 794       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
 795                 err_msg("Package: %s found in module %s, not in module: %s",
 796                         package_entry->name()->as_C_string(),
 797                         package_entry->module()->name()->as_C_string(),
 798                         module_entry->name()->as_C_string()));
 799     }
 800 
 801     log_debug(modules)("add_module_exports_to_all_unnamed(): package %s in module"
 802                        " %s is exported to all unnamed modules",
 803                        package_entry->name()->as_C_string(),
 804                        module_entry->name()->as_C_string());
 805 
 806     // Mark package as exported to all unnamed modules, unless already
 807     // unqualifiedly exported.
< prev index next >