262 }
263 }
264 }
265
266 // Caller needs ResourceMark.
267 static void throw_dup_pkg_exception(const char* module_name, PackageEntry* package, TRAPS) {
268 const char* package_name = package->name()->as_C_string();
269 if (package->module()->is_named()) {
270 THROW_MSG(vmSymbols::java_lang_IllegalStateException(),
271 err_msg("Package %s for module %s is already in another module, %s, defined to the class loader",
272 package_name, module_name, package->module()->name()->as_C_string()));
273 } else {
274 THROW_MSG(vmSymbols::java_lang_IllegalStateException(),
275 err_msg("Package %s for module %s is already in the unnamed module defined to the class loader",
276 package_name, module_name));
277 }
278 }
279
280 void Modules::define_module(Handle module, jboolean is_open, jstring version,
281 jstring location, jobjectArray packages, TRAPS) {
282 check_cds_restrictions(CHECK);
283 ResourceMark rm(THREAD);
284
285 if (module.is_null()) {
286 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
287 }
288
289 if (!java_lang_Module::is_instance(module())) {
290 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
291 "module is not an instance of type java.lang.Module");
292 }
293
294 int module_name_len;
295 char* module_name = get_module_name(module(), module_name_len, CHECK);
296 if (module_name == nullptr) {
297 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
298 "Module name cannot be null");
299 }
300
301 // Resolve packages
302 objArrayHandle packages_h(THREAD, objArrayOop(JNIHandles::resolve(packages)));
303 int num_packages = (packages_h.is_null() ? 0 : packages_h->length());
304
305 // Special handling of java.base definition
306 if (strcmp(module_name, JAVA_BASE_NAME) == 0) {
307 assert(is_open == JNI_FALSE, "java.base module cannot be open");
308 define_javabase_module(module, version, location, packages_h, num_packages, CHECK);
309 return;
310 }
311
312 oop loader = java_lang_Module::loader(module());
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
319 // Only modules defined to either the boot or platform class loader, can define a "java/" package.
320 bool java_pkg_disallowed = !h_loader.is_null() &&
321 !SystemDictionary::is_platform_class_loader(h_loader());
322
323 // Check that the list of packages has no duplicates and that the
721 }
722
723 if (h_system_loader.is_null()) {
724 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null system loader object");
725 }
726
727 ClassLoaderData* platform_loader_data = SystemDictionary::register_loader(h_platform_loader);
728 SystemDictionary::set_platform_loader(platform_loader_data);
729 ClassLoaderDataShared::restore_java_platform_loader_from_archive(platform_loader_data);
730
731 ClassLoaderData* system_loader_data = SystemDictionary::register_loader(h_system_loader);
732 SystemDictionary::set_system_loader(system_loader_data);
733 // system_loader_data here is always an instance of jdk.internal.loader.ClassLoader$AppClassLoader.
734 // However, if -Djava.system.class.loader=xxx is specified, java_platform_loader() would
735 // be an instance of a user-defined class, so make sure this never happens.
736 assert(Arguments::get_property("java.system.class.loader") == nullptr,
737 "archived full module should have been disabled if -Djava.system.class.loader is specified");
738 ClassLoaderDataShared::restore_java_system_loader_from_archive(system_loader_data);
739 }
740
741 void Modules::check_cds_restrictions(TRAPS) {
742 if (CDSConfig::is_dumping_full_module_graph() && Universe::is_module_initialized()) {
743 THROW_MSG(vmSymbols::java_lang_UnsupportedOperationException(),
744 "During -Xshare:dump, module system cannot be modified after it's initialized");
745 }
746 }
747 #endif // INCLUDE_CDS_JAVA_HEAP
748
749 void Modules::set_bootloader_unnamed_module(Handle module, TRAPS) {
750 ResourceMark rm(THREAD);
751
752 if (module.is_null()) {
753 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
754 }
755 if (!java_lang_Module::is_instance(module())) {
756 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
757 "module is not an instance of type java.lang.Module");
758 }
759
760 // Ensure that this is an unnamed module
761 oop name = java_lang_Module::name(module());
762 if (name != nullptr) {
763 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
764 "boot loader's unnamed module's java.lang.Module has a name");
765 }
766
767 // Validate java_base's loader is the boot loader.
768 oop loader = java_lang_Module::loader(module());
769 if (loader != nullptr) {
770 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
771 "Class loader must be the boot class loader");
772 }
773
774 log_debug(module)("set_bootloader_unnamed_module(): recording unnamed module for boot loader");
775
776 // Set java.lang.Module for the boot loader's unnamed module
777 ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data();
778 ModuleEntry* unnamed_module = boot_loader_data->unnamed_module();
779 assert(unnamed_module != nullptr, "boot loader's unnamed ModuleEntry not defined");
780 unnamed_module->set_module(boot_loader_data->add_handle(module));
781 // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module object.
782 java_lang_Module::set_module_entry(module(), unnamed_module);
783 }
784
785 void Modules::add_module_exports(Handle from_module, jstring package_name, Handle to_module, TRAPS) {
786 check_cds_restrictions(CHECK);
787
788 if (package_name == nullptr) {
789 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
790 "package is null");
791 }
792 if (from_module.is_null()) {
793 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
794 "from_module is null");
795 }
796 ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
797 if (from_module_entry == nullptr) {
798 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
799 "from_module cannot be found");
800 }
801
802 // All packages in unnamed and open modules are exported by default.
803 if (!from_module_entry->is_named() || from_module_entry->is_open()) return;
804
805 ModuleEntry* to_module_entry;
806 if (to_module.is_null()) {
834 if (package_entry == nullptr) {
835 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
836 err_msg("Package %s not found in from_module %s",
837 pkg != nullptr ? pkg : "",
838 from_module_entry->name()->as_C_string()));
839 }
840
841 if (log_is_enabled(Debug, module)) {
842 log_debug(module)("add_module_exports(): package %s in module %s is exported to module %s",
843 package_entry->name()->as_C_string(),
844 from_module_entry->name()->as_C_string(),
845 to_module_entry == nullptr ? "null" :
846 to_module_entry->is_named() ?
847 to_module_entry->name()->as_C_string() : UNNAMED_MODULE);
848 }
849 }
850
851
852 void Modules::add_module_exports_qualified(Handle from_module, jstring package,
853 Handle to_module, TRAPS) {
854 check_cds_restrictions(CHECK);
855 if (to_module.is_null()) {
856 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
857 "to_module is null");
858 }
859 add_module_exports(from_module, package, to_module, CHECK);
860 }
861
862 void Modules::add_reads_module(Handle from_module, Handle to_module, TRAPS) {
863 check_cds_restrictions(CHECK);
864 if (from_module.is_null()) {
865 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
866 "from_module is null");
867 }
868
869 ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
870 if (from_module_entry == nullptr) {
871 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
872 "from_module is not valid");
873 }
874
875 ModuleEntry* to_module_entry;
876 if (!to_module.is_null()) {
877 to_module_entry = get_module_entry(to_module, CHECK);
878 if (to_module_entry == nullptr) {
879 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
880 "to_module is invalid");
881 }
882 } else {
883 to_module_entry = nullptr;
949 assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()),
950 "Class loader is not a subclass of java.lang.ClassLoader");
951 assert(package_name != nullptr, "the package_name should not be null");
952
953 if (strlen(package_name) == 0) {
954 return nullptr;
955 }
956 TempNewSymbol package_sym = SymbolTable::new_symbol(package_name);
957 const PackageEntry* const pkg_entry =
958 get_package_entry_by_name(package_sym, h_loader);
959 const ModuleEntry* const module_entry = (pkg_entry != nullptr ? pkg_entry->module() : nullptr);
960
961 if (module_entry != nullptr && module_entry->module() != nullptr && module_entry->is_named()) {
962 return module_entry->module();
963 }
964 return nullptr;
965 }
966
967 // Export package in module to all unnamed modules.
968 void Modules::add_module_exports_to_all_unnamed(Handle module, jstring package_name, TRAPS) {
969 check_cds_restrictions(CHECK);
970 if (module.is_null()) {
971 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
972 "module is null");
973 }
974 if (package_name == nullptr) {
975 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
976 "package is null");
977 }
978 ModuleEntry* module_entry = get_module_entry(module, CHECK);
979 if (module_entry == nullptr) {
980 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
981 "module is invalid");
982 }
983
984 // No-op for unnamed module and open modules
985 if (!module_entry->is_named() || module_entry->is_open())
986 return;
987
988 ResourceMark rm(THREAD);
989 char buf[128];
|
262 }
263 }
264 }
265
266 // Caller needs ResourceMark.
267 static void throw_dup_pkg_exception(const char* module_name, PackageEntry* package, TRAPS) {
268 const char* package_name = package->name()->as_C_string();
269 if (package->module()->is_named()) {
270 THROW_MSG(vmSymbols::java_lang_IllegalStateException(),
271 err_msg("Package %s for module %s is already in another module, %s, defined to the class loader",
272 package_name, module_name, package->module()->name()->as_C_string()));
273 } else {
274 THROW_MSG(vmSymbols::java_lang_IllegalStateException(),
275 err_msg("Package %s for module %s is already in the unnamed module defined to the class loader",
276 package_name, module_name));
277 }
278 }
279
280 void Modules::define_module(Handle module, jboolean is_open, jstring version,
281 jstring location, jobjectArray packages, TRAPS) {
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 if (strncmp(module_name, "jdk.proxy", 9) != 0) {
304 check_cds_restrictions(Handle(), Handle(), CHECK);
305 }
306
307 // Special handling of java.base definition
308 if (strcmp(module_name, JAVA_BASE_NAME) == 0) {
309 assert(is_open == JNI_FALSE, "java.base module cannot be open");
310 define_javabase_module(module, version, location, packages_h, num_packages, CHECK);
311 return;
312 }
313
314 oop loader = java_lang_Module::loader(module());
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
321 // Only modules defined to either the boot or platform class loader, can define a "java/" package.
322 bool java_pkg_disallowed = !h_loader.is_null() &&
323 !SystemDictionary::is_platform_class_loader(h_loader());
324
325 // Check that the list of packages has no duplicates and that the
723 }
724
725 if (h_system_loader.is_null()) {
726 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null system loader object");
727 }
728
729 ClassLoaderData* platform_loader_data = SystemDictionary::register_loader(h_platform_loader);
730 SystemDictionary::set_platform_loader(platform_loader_data);
731 ClassLoaderDataShared::restore_java_platform_loader_from_archive(platform_loader_data);
732
733 ClassLoaderData* system_loader_data = SystemDictionary::register_loader(h_system_loader);
734 SystemDictionary::set_system_loader(system_loader_data);
735 // system_loader_data here is always an instance of jdk.internal.loader.ClassLoader$AppClassLoader.
736 // However, if -Djava.system.class.loader=xxx is specified, java_platform_loader() would
737 // be an instance of a user-defined class, so make sure this never happens.
738 assert(Arguments::get_property("java.system.class.loader") == nullptr,
739 "archived full module should have been disabled if -Djava.system.class.loader is specified");
740 ClassLoaderDataShared::restore_java_system_loader_from_archive(system_loader_data);
741 }
742
743 void Modules::check_cds_restrictions(Handle module1, Handle module2, TRAPS) {
744 if (CDSConfig::is_dumping_full_module_graph() && Universe::is_module_initialized()) {
745 if (CDSConfig::is_dumping_dynamic_proxies() && (is_dynamic_proxy_module(module1) || is_dynamic_proxy_module(module2))) {
746 // The only the we allow is to add or modify the jdk.proxy?? modules that are used for dynamic proxies.
747 } else {
748 THROW_MSG(vmSymbols::java_lang_UnsupportedOperationException(),
749 "During -Xshare:dump, module system cannot be modified after it's initialized");
750 }
751 }
752 }
753
754 #endif // INCLUDE_CDS_JAVA_HEAP
755
756 bool Modules::is_dynamic_proxy_module(Handle module) {
757 if (!module.is_null()) {
758 ModuleEntry* module_entry = java_lang_Module::module_entry(module());
759 return is_dynamic_proxy_module(module_entry);
760 }
761 return false;
762 }
763
764 bool Modules::is_dynamic_proxy_module(ModuleEntry* module_entry) {
765 return (module_entry != nullptr && module_entry->is_named() && module_entry->name()->starts_with("jdk.proxy"));
766 }
767
768 void Modules::set_bootloader_unnamed_module(Handle module, TRAPS) {
769 ResourceMark rm(THREAD);
770
771 if (module.is_null()) {
772 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
773 }
774 if (!java_lang_Module::is_instance(module())) {
775 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
776 "module is not an instance of type java.lang.Module");
777 }
778
779 // Ensure that this is an unnamed module
780 oop name = java_lang_Module::name(module());
781 if (name != nullptr) {
782 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
783 "boot loader's unnamed module's java.lang.Module has a name");
784 }
785
786 // Validate java_base's loader is the boot loader.
787 oop loader = java_lang_Module::loader(module());
788 if (loader != nullptr) {
789 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
790 "Class loader must be the boot class loader");
791 }
792
793 log_debug(module)("set_bootloader_unnamed_module(): recording unnamed module for boot loader");
794
795 // Set java.lang.Module for the boot loader's unnamed module
796 ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data();
797 ModuleEntry* unnamed_module = boot_loader_data->unnamed_module();
798 assert(unnamed_module != nullptr, "boot loader's unnamed ModuleEntry not defined");
799 unnamed_module->set_module(boot_loader_data->add_handle(module));
800 // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module object.
801 java_lang_Module::set_module_entry(module(), unnamed_module);
802 }
803
804 void Modules::add_module_exports(Handle from_module, jstring package_name, Handle to_module, TRAPS) {
805 check_cds_restrictions(from_module, to_module, CHECK);
806
807 if (package_name == nullptr) {
808 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
809 "package is null");
810 }
811 if (from_module.is_null()) {
812 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
813 "from_module is null");
814 }
815 ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
816 if (from_module_entry == nullptr) {
817 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
818 "from_module cannot be found");
819 }
820
821 // All packages in unnamed and open modules are exported by default.
822 if (!from_module_entry->is_named() || from_module_entry->is_open()) return;
823
824 ModuleEntry* to_module_entry;
825 if (to_module.is_null()) {
853 if (package_entry == nullptr) {
854 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
855 err_msg("Package %s not found in from_module %s",
856 pkg != nullptr ? pkg : "",
857 from_module_entry->name()->as_C_string()));
858 }
859
860 if (log_is_enabled(Debug, module)) {
861 log_debug(module)("add_module_exports(): package %s in module %s is exported to module %s",
862 package_entry->name()->as_C_string(),
863 from_module_entry->name()->as_C_string(),
864 to_module_entry == nullptr ? "null" :
865 to_module_entry->is_named() ?
866 to_module_entry->name()->as_C_string() : UNNAMED_MODULE);
867 }
868 }
869
870
871 void Modules::add_module_exports_qualified(Handle from_module, jstring package,
872 Handle to_module, TRAPS) {
873 check_cds_restrictions(from_module, to_module, CHECK);
874 if (to_module.is_null()) {
875 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
876 "to_module is null");
877 }
878 add_module_exports(from_module, package, to_module, CHECK);
879 }
880
881 void Modules::add_reads_module(Handle from_module, Handle to_module, TRAPS) {
882 check_cds_restrictions(from_module, to_module, CHECK);
883 if (from_module.is_null()) {
884 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
885 "from_module is null");
886 }
887
888 ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
889 if (from_module_entry == nullptr) {
890 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
891 "from_module is not valid");
892 }
893
894 ModuleEntry* to_module_entry;
895 if (!to_module.is_null()) {
896 to_module_entry = get_module_entry(to_module, CHECK);
897 if (to_module_entry == nullptr) {
898 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
899 "to_module is invalid");
900 }
901 } else {
902 to_module_entry = nullptr;
968 assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()),
969 "Class loader is not a subclass of java.lang.ClassLoader");
970 assert(package_name != nullptr, "the package_name should not be null");
971
972 if (strlen(package_name) == 0) {
973 return nullptr;
974 }
975 TempNewSymbol package_sym = SymbolTable::new_symbol(package_name);
976 const PackageEntry* const pkg_entry =
977 get_package_entry_by_name(package_sym, h_loader);
978 const ModuleEntry* const module_entry = (pkg_entry != nullptr ? pkg_entry->module() : nullptr);
979
980 if (module_entry != nullptr && module_entry->module() != nullptr && module_entry->is_named()) {
981 return module_entry->module();
982 }
983 return nullptr;
984 }
985
986 // Export package in module to all unnamed modules.
987 void Modules::add_module_exports_to_all_unnamed(Handle module, jstring package_name, TRAPS) {
988 check_cds_restrictions(Handle(), module, CHECK);
989 if (module.is_null()) {
990 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
991 "module is null");
992 }
993 if (package_name == nullptr) {
994 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
995 "package is null");
996 }
997 ModuleEntry* module_entry = get_module_entry(module, CHECK);
998 if (module_entry == nullptr) {
999 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1000 "module is invalid");
1001 }
1002
1003 // No-op for unnamed module and open modules
1004 if (!module_entry->is_named() || module_entry->is_open())
1005 return;
1006
1007 ResourceMark rm(THREAD);
1008 char buf[128];
|