< prev index next >

src/hotspot/share/classfile/modules.cpp

Print this page

257     }
258   }
259 }
260 
261 // Caller needs ResourceMark.
262 static void throw_dup_pkg_exception(const char* module_name, PackageEntry* package, TRAPS) {
263   const char* package_name = package->name()->as_C_string();
264   if (package->module()->is_named()) {
265     THROW_MSG(vmSymbols::java_lang_IllegalStateException(),
266       err_msg("Package %s for module %s is already in another module, %s, defined to the class loader",
267               package_name, module_name, package->module()->name()->as_C_string()));
268   } else {
269     THROW_MSG(vmSymbols::java_lang_IllegalStateException(),
270       err_msg("Package %s for module %s is already in the unnamed module defined to the class loader",
271               package_name, module_name));
272   }
273 }
274 
275 void Modules::define_module(Handle module, jboolean is_open, jstring version,
276                             jstring location, jobjectArray packages, TRAPS) {
277   check_cds_restrictions(CHECK);
278   ResourceMark rm(THREAD);
279 
280   if (module.is_null()) {
281     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
282   }
283 
284   if (!java_lang_Module::is_instance(module())) {
285     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
286               "module is not an instance of type java.lang.Module");
287   }
288 
289   int module_name_len;
290   char* module_name = get_module_name(module(), module_name_len, CHECK);
291   if (module_name == nullptr) {
292     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
293               "Module name cannot be null");
294   }
295 
296   // Resolve packages
297   objArrayHandle packages_h(THREAD, objArrayOop(JNIHandles::resolve(packages)));
298   int num_packages = (packages_h.is_null() ? 0 : packages_h->length());



299 
300   // Special handling of java.base definition
301   if (strcmp(module_name, JAVA_BASE_NAME) == 0) {
302     assert(is_open == JNI_FALSE, "java.base module cannot be open");
303     define_javabase_module(module, version, location, packages_h, num_packages, CHECK);
304     return;
305   }
306 
307   oop loader = java_lang_Module::loader(module());
308   // Make sure loader is not the jdk.internal.reflect.DelegatingClassLoader.
309   if (loader != java_lang_ClassLoader::non_reflection_class_loader(loader)) {
310     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
311               "Class loader is an invalid delegating class loader");
312   }
313   Handle h_loader = Handle(THREAD, loader);
314   // define_module can be called during start-up, before the class loader's ClassLoaderData
315   // has been created.  SystemDictionary::register_loader ensures creation, if needed.
316   ClassLoaderData* loader_data = SystemDictionary::register_loader(h_loader);
317   assert(loader_data != nullptr, "class loader data shouldn't be null");
318 

622   }
623 
624   if (h_system_loader.is_null()) {
625     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null system loader object");
626   }
627 
628   ClassLoaderData* platform_loader_data = SystemDictionary::register_loader(h_platform_loader);
629   SystemDictionary::set_platform_loader(platform_loader_data);
630   ClassLoaderDataShared::restore_java_platform_loader_from_archive(platform_loader_data);
631 
632   ClassLoaderData* system_loader_data = SystemDictionary::register_loader(h_system_loader);
633   SystemDictionary::set_system_loader(system_loader_data);
634   // system_loader_data here is always an instance of jdk.internal.loader.ClassLoader$AppClassLoader.
635   // However, if -Djava.system.class.loader=xxx is specified, java_platform_loader() would
636   // be an instance of a user-defined class, so make sure this never happens.
637   assert(Arguments::get_property("java.system.class.loader") == nullptr,
638            "archived full module should have been disabled if -Djava.system.class.loader is specified");
639   ClassLoaderDataShared::restore_java_system_loader_from_archive(system_loader_data);
640 }
641 
642 void Modules::check_cds_restrictions(TRAPS) {
643   if (CDSConfig::is_dumping_full_module_graph() && Universe::is_module_initialized()) {
644     THROW_MSG(vmSymbols::java_lang_UnsupportedOperationException(),
645               "During -Xshare:dump, module system cannot be modified after it's initialized");




646   }
647 }













648 #endif // INCLUDE_CDS_JAVA_HEAP
649 
650 void Modules::set_bootloader_unnamed_module(Handle module, TRAPS) {
651   ResourceMark rm(THREAD);
652 
653   if (module.is_null()) {
654     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
655   }
656   if (!java_lang_Module::is_instance(module())) {
657     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
658               "module is not an instance of type java.lang.Module");
659   }
660 
661   // Ensure that this is an unnamed module
662   oop name = java_lang_Module::name(module());
663   if (name != nullptr) {
664     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
665               "boot loader's unnamed module's java.lang.Module has a name");
666   }
667 
668   // Validate java_base's loader is the boot loader.
669   oop loader = java_lang_Module::loader(module());
670   if (loader != nullptr) {
671     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
672               "Class loader must be the boot class loader");
673   }
674 
675   log_debug(module)("set_bootloader_unnamed_module(): recording unnamed module for boot loader");
676 
677   // Set java.lang.Module for the boot loader's unnamed module
678   ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data();
679   ModuleEntry* unnamed_module = boot_loader_data->unnamed_module();
680   assert(unnamed_module != nullptr, "boot loader's unnamed ModuleEntry not defined");
681   unnamed_module->set_module(boot_loader_data->add_handle(module));
682   // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module object.
683   java_lang_Module::set_module_entry(module(), unnamed_module);
684 }
685 
686 void Modules::add_module_exports(Handle from_module, jstring package_name, Handle to_module, TRAPS) {
687   check_cds_restrictions(CHECK);
688 
689   if (package_name == nullptr) {
690     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
691               "package is null");
692   }
693   if (from_module.is_null()) {
694     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
695               "from_module is null");
696   }
697   ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
698   if (from_module_entry == nullptr) {
699     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
700               "from_module cannot be found");
701   }
702 
703   // All packages in unnamed and open modules are exported by default.
704   if (!from_module_entry->is_named() || from_module_entry->is_open()) return;
705 
706   ModuleEntry* to_module_entry;
707   if (to_module.is_null()) {

735   if (package_entry == nullptr) {
736     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
737               err_msg("Package %s not found in from_module %s",
738                       pkg != nullptr ? pkg : "",
739                       from_module_entry->name()->as_C_string()));
740   }
741 
742   if (log_is_enabled(Debug, module)) {
743     log_debug(module)("add_module_exports(): package %s in module %s is exported to module %s",
744                       package_entry->name()->as_C_string(),
745                       from_module_entry->name()->as_C_string(),
746                       to_module_entry == nullptr ? "null" :
747                       to_module_entry->is_named() ?
748                       to_module_entry->name()->as_C_string() : UNNAMED_MODULE);
749   }
750 }
751 
752 
753 void Modules::add_module_exports_qualified(Handle from_module, jstring package,
754                                            Handle to_module, TRAPS) {
755   check_cds_restrictions(CHECK);
756   if (to_module.is_null()) {
757     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
758               "to_module is null");
759   }
760   add_module_exports(from_module, package, to_module, CHECK);
761 }
762 
763 void Modules::add_reads_module(Handle from_module, Handle to_module, TRAPS) {
764   check_cds_restrictions(CHECK);
765   if (from_module.is_null()) {
766     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
767               "from_module is null");
768   }
769 
770   ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
771   if (from_module_entry == nullptr) {
772     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
773               "from_module is not valid");
774   }
775 
776   ModuleEntry* to_module_entry;
777   if (!to_module.is_null()) {
778     to_module_entry = get_module_entry(to_module, CHECK);
779     if (to_module_entry == nullptr) {
780       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
781                 "to_module is invalid");
782     }
783   } else {
784     to_module_entry = nullptr;

850   assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()),
851          "Class loader is not a subclass of java.lang.ClassLoader");
852   assert(package_name != nullptr, "the package_name should not be null");
853 
854   if (strlen(package_name) == 0) {
855     return nullptr;
856   }
857   TempNewSymbol package_sym = SymbolTable::new_symbol(package_name);
858   const PackageEntry* const pkg_entry =
859     get_package_entry_by_name(package_sym, h_loader);
860   const ModuleEntry* const module_entry = (pkg_entry != nullptr ? pkg_entry->module() : nullptr);
861 
862   if (module_entry != nullptr && module_entry->module() != nullptr && module_entry->is_named()) {
863     return module_entry->module();
864   }
865   return nullptr;
866 }
867 
868 // Export package in module to all unnamed modules.
869 void Modules::add_module_exports_to_all_unnamed(Handle module, jstring package_name, TRAPS) {
870   check_cds_restrictions(CHECK);
871   if (module.is_null()) {
872     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
873               "module is null");
874   }
875   if (package_name == nullptr) {
876     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
877               "package is null");
878   }
879   ModuleEntry* module_entry = get_module_entry(module, CHECK);
880   if (module_entry == nullptr) {
881     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
882               "module is invalid");
883   }
884 
885   // No-op for unnamed module and open modules
886   if (!module_entry->is_named() || module_entry->is_open())
887     return;
888 
889   ResourceMark rm(THREAD);
890   char buf[128];

257     }
258   }
259 }
260 
261 // Caller needs ResourceMark.
262 static void throw_dup_pkg_exception(const char* module_name, PackageEntry* package, TRAPS) {
263   const char* package_name = package->name()->as_C_string();
264   if (package->module()->is_named()) {
265     THROW_MSG(vmSymbols::java_lang_IllegalStateException(),
266       err_msg("Package %s for module %s is already in another module, %s, defined to the class loader",
267               package_name, module_name, package->module()->name()->as_C_string()));
268   } else {
269     THROW_MSG(vmSymbols::java_lang_IllegalStateException(),
270       err_msg("Package %s for module %s is already in the unnamed module defined to the class loader",
271               package_name, module_name));
272   }
273 }
274 
275 void Modules::define_module(Handle module, jboolean is_open, jstring version,
276                             jstring location, jobjectArray packages, TRAPS) {

277   ResourceMark rm(THREAD);
278 
279   if (module.is_null()) {
280     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
281   }
282 
283   if (!java_lang_Module::is_instance(module())) {
284     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
285               "module is not an instance of type java.lang.Module");
286   }
287 
288   int module_name_len;
289   char* module_name = get_module_name(module(), module_name_len, CHECK);
290   if (module_name == nullptr) {
291     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
292               "Module name cannot be null");
293   }
294 
295   // Resolve packages
296   objArrayHandle packages_h(THREAD, objArrayOop(JNIHandles::resolve(packages)));
297   int num_packages = (packages_h.is_null() ? 0 : packages_h->length());
298   if (strncmp(module_name, "jdk.proxy", 9) != 0) {
299     check_cds_restrictions(Handle(), Handle(), CHECK);
300   }
301 
302   // Special handling of java.base definition
303   if (strcmp(module_name, JAVA_BASE_NAME) == 0) {
304     assert(is_open == JNI_FALSE, "java.base module cannot be open");
305     define_javabase_module(module, version, location, packages_h, num_packages, CHECK);
306     return;
307   }
308 
309   oop loader = java_lang_Module::loader(module());
310   // Make sure loader is not the jdk.internal.reflect.DelegatingClassLoader.
311   if (loader != java_lang_ClassLoader::non_reflection_class_loader(loader)) {
312     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
313               "Class loader is an invalid delegating class loader");
314   }
315   Handle h_loader = Handle(THREAD, loader);
316   // define_module can be called during start-up, before the class loader's ClassLoaderData
317   // has been created.  SystemDictionary::register_loader ensures creation, if needed.
318   ClassLoaderData* loader_data = SystemDictionary::register_loader(h_loader);
319   assert(loader_data != nullptr, "class loader data shouldn't be null");
320 

624   }
625 
626   if (h_system_loader.is_null()) {
627     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null system loader object");
628   }
629 
630   ClassLoaderData* platform_loader_data = SystemDictionary::register_loader(h_platform_loader);
631   SystemDictionary::set_platform_loader(platform_loader_data);
632   ClassLoaderDataShared::restore_java_platform_loader_from_archive(platform_loader_data);
633 
634   ClassLoaderData* system_loader_data = SystemDictionary::register_loader(h_system_loader);
635   SystemDictionary::set_system_loader(system_loader_data);
636   // system_loader_data here is always an instance of jdk.internal.loader.ClassLoader$AppClassLoader.
637   // However, if -Djava.system.class.loader=xxx is specified, java_platform_loader() would
638   // be an instance of a user-defined class, so make sure this never happens.
639   assert(Arguments::get_property("java.system.class.loader") == nullptr,
640            "archived full module should have been disabled if -Djava.system.class.loader is specified");
641   ClassLoaderDataShared::restore_java_system_loader_from_archive(system_loader_data);
642 }
643 
644 void Modules::check_cds_restrictions(Handle module1, Handle module2, TRAPS) {
645   if (CDSConfig::is_dumping_full_module_graph() && Universe::is_module_initialized()) {
646     if (ArchiveDynamicProxies && (is_dynamic_proxy_module(module1) || is_dynamic_proxy_module(module2))) {
647       // The only the we allow is to add or modify the jdk.proxy?? modules that are used for dynamic proxies.
648     } else {
649       THROW_MSG(vmSymbols::java_lang_UnsupportedOperationException(),
650                 "During -Xshare:dump, module system cannot be modified after it's initialized");
651     }
652   }
653 }
654 
655 bool Modules::is_dynamic_proxy_module(Handle module) {
656   if (!module.is_null()) {
657     ModuleEntry* module_entry = java_lang_Module::module_entry(module());
658     return is_dynamic_proxy_module(module_entry);
659   }
660   return false;
661 }
662 
663 bool Modules::is_dynamic_proxy_module(ModuleEntry* module_entry) {
664   return (module_entry != nullptr && module_entry->is_named() && module_entry->name()->starts_with("jdk.proxy"));
665 }
666 
667 #endif // INCLUDE_CDS_JAVA_HEAP
668 
669 void Modules::set_bootloader_unnamed_module(Handle module, TRAPS) {
670   ResourceMark rm(THREAD);
671 
672   if (module.is_null()) {
673     THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
674   }
675   if (!java_lang_Module::is_instance(module())) {
676     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
677               "module is not an instance of type java.lang.Module");
678   }
679 
680   // Ensure that this is an unnamed module
681   oop name = java_lang_Module::name(module());
682   if (name != nullptr) {
683     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
684               "boot loader's unnamed module's java.lang.Module has a name");
685   }
686 
687   // Validate java_base's loader is the boot loader.
688   oop loader = java_lang_Module::loader(module());
689   if (loader != nullptr) {
690     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
691               "Class loader must be the boot class loader");
692   }
693 
694   log_debug(module)("set_bootloader_unnamed_module(): recording unnamed module for boot loader");
695 
696   // Set java.lang.Module for the boot loader's unnamed module
697   ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data();
698   ModuleEntry* unnamed_module = boot_loader_data->unnamed_module();
699   assert(unnamed_module != nullptr, "boot loader's unnamed ModuleEntry not defined");
700   unnamed_module->set_module(boot_loader_data->add_handle(module));
701   // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module object.
702   java_lang_Module::set_module_entry(module(), unnamed_module);
703 }
704 
705 void Modules::add_module_exports(Handle from_module, jstring package_name, Handle to_module, TRAPS) {
706   check_cds_restrictions(from_module, to_module, CHECK);
707 
708   if (package_name == nullptr) {
709     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
710               "package is null");
711   }
712   if (from_module.is_null()) {
713     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
714               "from_module is null");
715   }
716   ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
717   if (from_module_entry == nullptr) {
718     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
719               "from_module cannot be found");
720   }
721 
722   // All packages in unnamed and open modules are exported by default.
723   if (!from_module_entry->is_named() || from_module_entry->is_open()) return;
724 
725   ModuleEntry* to_module_entry;
726   if (to_module.is_null()) {

754   if (package_entry == nullptr) {
755     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
756               err_msg("Package %s not found in from_module %s",
757                       pkg != nullptr ? pkg : "",
758                       from_module_entry->name()->as_C_string()));
759   }
760 
761   if (log_is_enabled(Debug, module)) {
762     log_debug(module)("add_module_exports(): package %s in module %s is exported to module %s",
763                       package_entry->name()->as_C_string(),
764                       from_module_entry->name()->as_C_string(),
765                       to_module_entry == nullptr ? "null" :
766                       to_module_entry->is_named() ?
767                       to_module_entry->name()->as_C_string() : UNNAMED_MODULE);
768   }
769 }
770 
771 
772 void Modules::add_module_exports_qualified(Handle from_module, jstring package,
773                                            Handle to_module, TRAPS) {
774   check_cds_restrictions(from_module, to_module, CHECK);
775   if (to_module.is_null()) {
776     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
777               "to_module is null");
778   }
779   add_module_exports(from_module, package, to_module, CHECK);
780 }
781 
782 void Modules::add_reads_module(Handle from_module, Handle to_module, TRAPS) {
783   check_cds_restrictions(from_module, to_module, CHECK);
784   if (from_module.is_null()) {
785     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
786               "from_module is null");
787   }
788 
789   ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
790   if (from_module_entry == nullptr) {
791     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
792               "from_module is not valid");
793   }
794 
795   ModuleEntry* to_module_entry;
796   if (!to_module.is_null()) {
797     to_module_entry = get_module_entry(to_module, CHECK);
798     if (to_module_entry == nullptr) {
799       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
800                 "to_module is invalid");
801     }
802   } else {
803     to_module_entry = nullptr;

869   assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()),
870          "Class loader is not a subclass of java.lang.ClassLoader");
871   assert(package_name != nullptr, "the package_name should not be null");
872 
873   if (strlen(package_name) == 0) {
874     return nullptr;
875   }
876   TempNewSymbol package_sym = SymbolTable::new_symbol(package_name);
877   const PackageEntry* const pkg_entry =
878     get_package_entry_by_name(package_sym, h_loader);
879   const ModuleEntry* const module_entry = (pkg_entry != nullptr ? pkg_entry->module() : nullptr);
880 
881   if (module_entry != nullptr && module_entry->module() != nullptr && module_entry->is_named()) {
882     return module_entry->module();
883   }
884   return nullptr;
885 }
886 
887 // Export package in module to all unnamed modules.
888 void Modules::add_module_exports_to_all_unnamed(Handle module, jstring package_name, TRAPS) {
889   check_cds_restrictions(Handle(), module, CHECK);
890   if (module.is_null()) {
891     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
892               "module is null");
893   }
894   if (package_name == nullptr) {
895     THROW_MSG(vmSymbols::java_lang_NullPointerException(),
896               "package is null");
897   }
898   ModuleEntry* module_entry = get_module_entry(module, CHECK);
899   if (module_entry == nullptr) {
900     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
901               "module is invalid");
902   }
903 
904   // No-op for unnamed module and open modules
905   if (!module_entry->is_named() || module_entry->is_open())
906     return;
907 
908   ResourceMark rm(THREAD);
909   char buf[128];
< prev index next >