< prev index next >

src/hotspot/share/classfile/modules.cpp

Print this page

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



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

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(TRAPS) {
645   if (CDSConfig::is_dumping_full_module_graph() && Universe::is_module_initialized()) {
646     THROW_MSG(vmSymbols::java_lang_UnsupportedOperationException(),
647               "During -Xshare:dump, module system cannot be modified after it's initialized");




648   }
649 }

650 #endif // INCLUDE_CDS_JAVA_HEAP
651 












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

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

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

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

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

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

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

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