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
745 }
746
747 if (h_system_loader.is_null()) {
748 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null system loader object");
749 }
750
751 ClassLoaderData* platform_loader_data = SystemDictionary::register_loader(h_platform_loader);
752 SystemDictionary::set_platform_loader(platform_loader_data);
753 ClassLoaderDataShared::restore_java_platform_loader_from_archive(platform_loader_data);
754
755 ClassLoaderData* system_loader_data = SystemDictionary::register_loader(h_system_loader);
756 SystemDictionary::set_system_loader(system_loader_data);
757 // system_loader_data here is always an instance of jdk.internal.loader.ClassLoader$AppClassLoader.
758 // However, if -Djava.system.class.loader=xxx is specified, java_platform_loader() would
759 // be an instance of a user-defined class, so make sure this never happens.
760 assert(Arguments::get_property("java.system.class.loader") == nullptr,
761 "archived full module should have been disabled if -Djava.system.class.loader is specified");
762 ClassLoaderDataShared::restore_java_system_loader_from_archive(system_loader_data);
763 }
764
765 void Modules::check_cds_restrictions(TRAPS) {
766 if (CDSConfig::is_dumping_full_module_graph() && Universe::is_module_initialized()) {
767 THROW_MSG(vmSymbols::java_lang_UnsupportedOperationException(),
768 "During -Xshare:dump, module system cannot be modified after it's initialized");
769 }
770 }
771 #endif // INCLUDE_CDS_JAVA_HEAP
772
773 void Modules::set_bootloader_unnamed_module(Handle module, TRAPS) {
774 ResourceMark rm(THREAD);
775
776 if (module.is_null()) {
777 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
778 }
779 if (!java_lang_Module::is_instance(module())) {
780 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
781 "module is not an instance of type java.lang.Module");
782 }
783
784 // Ensure that this is an unnamed module
785 oop name = java_lang_Module::name(module());
786 if (name != nullptr) {
787 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
788 "boot loader's unnamed module's java.lang.Module has a name");
789 }
790
791 // Validate java_base's loader is the boot loader.
792 oop loader = java_lang_Module::loader(module());
793 if (loader != nullptr) {
794 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
795 "Class loader must be the boot class loader");
796 }
797
798 log_debug(module)("set_bootloader_unnamed_module(): recording unnamed module for boot loader");
799
800 // Set java.lang.Module for the boot loader's unnamed module
801 ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data();
802 ModuleEntry* unnamed_module = boot_loader_data->unnamed_module();
803 assert(unnamed_module != nullptr, "boot loader's unnamed ModuleEntry not defined");
804 unnamed_module->set_module(boot_loader_data->add_handle(module));
805 // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module object.
806 java_lang_Module::set_module_entry(module(), unnamed_module);
807 }
808
809 void Modules::add_module_exports(Handle from_module, jstring package_name, Handle to_module, TRAPS) {
810 check_cds_restrictions(CHECK);
811
812 if (package_name == nullptr) {
813 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
814 "package is null");
815 }
816 if (from_module.is_null()) {
817 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
818 "from_module is null");
819 }
820 ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
821 if (from_module_entry == nullptr) {
822 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
823 "from_module cannot be found");
824 }
825
826 // All packages in unnamed and open modules are exported by default.
827 if (!from_module_entry->is_named() || from_module_entry->is_open()) return;
828
829 ModuleEntry* to_module_entry;
830 if (to_module.is_null()) {
858 if (package_entry == nullptr) {
859 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
860 err_msg("Package %s not found in from_module %s",
861 pkg != nullptr ? pkg : "",
862 from_module_entry->name()->as_C_string()));
863 }
864
865 if (log_is_enabled(Debug, module)) {
866 log_debug(module)("add_module_exports(): package %s in module %s is exported to module %s",
867 package_entry->name()->as_C_string(),
868 from_module_entry->name()->as_C_string(),
869 to_module_entry == nullptr ? "null" :
870 to_module_entry->is_named() ?
871 to_module_entry->name()->as_C_string() : UNNAMED_MODULE);
872 }
873 }
874
875
876 void Modules::add_module_exports_qualified(Handle from_module, jstring package,
877 Handle to_module, TRAPS) {
878 check_cds_restrictions(CHECK);
879 if (to_module.is_null()) {
880 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
881 "to_module is null");
882 }
883 add_module_exports(from_module, package, to_module, CHECK);
884 }
885
886 void Modules::add_reads_module(Handle from_module, Handle to_module, TRAPS) {
887 check_cds_restrictions(CHECK);
888 if (from_module.is_null()) {
889 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
890 "from_module is null");
891 }
892
893 ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
894 if (from_module_entry == nullptr) {
895 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
896 "from_module is not valid");
897 }
898
899 ModuleEntry* to_module_entry;
900 if (!to_module.is_null()) {
901 to_module_entry = get_module_entry(to_module, CHECK);
902 if (to_module_entry == nullptr) {
903 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
904 "to_module is invalid");
905 }
906 } else {
907 to_module_entry = nullptr;
973 assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()),
974 "Class loader is not a subclass of java.lang.ClassLoader");
975 assert(package_name != nullptr, "the package_name should not be null");
976
977 if (strlen(package_name) == 0) {
978 return nullptr;
979 }
980 TempNewSymbol package_sym = SymbolTable::new_symbol(package_name);
981 const PackageEntry* const pkg_entry =
982 get_package_entry_by_name(package_sym, h_loader);
983 const ModuleEntry* const module_entry = (pkg_entry != nullptr ? pkg_entry->module() : nullptr);
984
985 if (module_entry != nullptr && module_entry->module() != nullptr && module_entry->is_named()) {
986 return module_entry->module();
987 }
988 return nullptr;
989 }
990
991 // Export package in module to all unnamed modules.
992 void Modules::add_module_exports_to_all_unnamed(Handle module, jstring package_name, TRAPS) {
993 check_cds_restrictions(CHECK);
994 if (module.is_null()) {
995 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
996 "module is null");
997 }
998 if (package_name == nullptr) {
999 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
1000 "package is null");
1001 }
1002 ModuleEntry* module_entry = get_module_entry(module, CHECK);
1003 if (module_entry == nullptr) {
1004 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1005 "module is invalid");
1006 }
1007
1008 // No-op for unnamed module and open modules
1009 if (!module_entry->is_named() || module_entry->is_open())
1010 return;
1011
1012 ResourceMark rm(THREAD);
1013 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
747 }
748
749 if (h_system_loader.is_null()) {
750 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null system loader object");
751 }
752
753 ClassLoaderData* platform_loader_data = SystemDictionary::register_loader(h_platform_loader);
754 SystemDictionary::set_platform_loader(platform_loader_data);
755 ClassLoaderDataShared::restore_java_platform_loader_from_archive(platform_loader_data);
756
757 ClassLoaderData* system_loader_data = SystemDictionary::register_loader(h_system_loader);
758 SystemDictionary::set_system_loader(system_loader_data);
759 // system_loader_data here is always an instance of jdk.internal.loader.ClassLoader$AppClassLoader.
760 // However, if -Djava.system.class.loader=xxx is specified, java_platform_loader() would
761 // be an instance of a user-defined class, so make sure this never happens.
762 assert(Arguments::get_property("java.system.class.loader") == nullptr,
763 "archived full module should have been disabled if -Djava.system.class.loader is specified");
764 ClassLoaderDataShared::restore_java_system_loader_from_archive(system_loader_data);
765 }
766
767 void Modules::check_cds_restrictions(Handle module1, Handle module2, TRAPS) {
768 if (CDSConfig::is_dumping_full_module_graph() && Universe::is_module_initialized()) {
769 if (CDSConfig::is_dumping_dynamic_proxies() && (is_dynamic_proxy_module(module1) || is_dynamic_proxy_module(module2))) {
770 // The only the we allow is to add or modify the jdk.proxy?? modules that are used for dynamic proxies.
771 } else {
772 THROW_MSG(vmSymbols::java_lang_UnsupportedOperationException(),
773 "During -Xshare:dump, module system cannot be modified after it's initialized");
774 }
775 }
776 }
777
778 #endif // INCLUDE_CDS_JAVA_HEAP
779
780 bool Modules::is_dynamic_proxy_module(Handle module) {
781 if (!module.is_null()) {
782 ModuleEntry* module_entry = java_lang_Module::module_entry(module());
783 return is_dynamic_proxy_module(module_entry);
784 }
785 return false;
786 }
787
788 bool Modules::is_dynamic_proxy_module(ModuleEntry* module_entry) {
789 return (module_entry != nullptr && module_entry->is_named() && module_entry->name()->starts_with("jdk.proxy"));
790 }
791
792 void Modules::set_bootloader_unnamed_module(Handle module, TRAPS) {
793 ResourceMark rm(THREAD);
794
795 if (module.is_null()) {
796 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object");
797 }
798 if (!java_lang_Module::is_instance(module())) {
799 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
800 "module is not an instance of type java.lang.Module");
801 }
802
803 // Ensure that this is an unnamed module
804 oop name = java_lang_Module::name(module());
805 if (name != nullptr) {
806 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
807 "boot loader's unnamed module's java.lang.Module has a name");
808 }
809
810 // Validate java_base's loader is the boot loader.
811 oop loader = java_lang_Module::loader(module());
812 if (loader != nullptr) {
813 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
814 "Class loader must be the boot class loader");
815 }
816
817 log_debug(module)("set_bootloader_unnamed_module(): recording unnamed module for boot loader");
818
819 // Set java.lang.Module for the boot loader's unnamed module
820 ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data();
821 ModuleEntry* unnamed_module = boot_loader_data->unnamed_module();
822 assert(unnamed_module != nullptr, "boot loader's unnamed ModuleEntry not defined");
823 unnamed_module->set_module(boot_loader_data->add_handle(module));
824 // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module object.
825 java_lang_Module::set_module_entry(module(), unnamed_module);
826 }
827
828 void Modules::add_module_exports(Handle from_module, jstring package_name, Handle to_module, TRAPS) {
829 check_cds_restrictions(from_module, to_module, CHECK);
830
831 if (package_name == nullptr) {
832 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
833 "package is null");
834 }
835 if (from_module.is_null()) {
836 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
837 "from_module is null");
838 }
839 ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
840 if (from_module_entry == nullptr) {
841 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
842 "from_module cannot be found");
843 }
844
845 // All packages in unnamed and open modules are exported by default.
846 if (!from_module_entry->is_named() || from_module_entry->is_open()) return;
847
848 ModuleEntry* to_module_entry;
849 if (to_module.is_null()) {
877 if (package_entry == nullptr) {
878 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
879 err_msg("Package %s not found in from_module %s",
880 pkg != nullptr ? pkg : "",
881 from_module_entry->name()->as_C_string()));
882 }
883
884 if (log_is_enabled(Debug, module)) {
885 log_debug(module)("add_module_exports(): package %s in module %s is exported to module %s",
886 package_entry->name()->as_C_string(),
887 from_module_entry->name()->as_C_string(),
888 to_module_entry == nullptr ? "null" :
889 to_module_entry->is_named() ?
890 to_module_entry->name()->as_C_string() : UNNAMED_MODULE);
891 }
892 }
893
894
895 void Modules::add_module_exports_qualified(Handle from_module, jstring package,
896 Handle to_module, TRAPS) {
897 check_cds_restrictions(from_module, to_module, CHECK);
898 if (to_module.is_null()) {
899 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
900 "to_module is null");
901 }
902 add_module_exports(from_module, package, to_module, CHECK);
903 }
904
905 void Modules::add_reads_module(Handle from_module, Handle to_module, TRAPS) {
906 check_cds_restrictions(from_module, to_module, CHECK);
907 if (from_module.is_null()) {
908 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
909 "from_module is null");
910 }
911
912 ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK);
913 if (from_module_entry == nullptr) {
914 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
915 "from_module is not valid");
916 }
917
918 ModuleEntry* to_module_entry;
919 if (!to_module.is_null()) {
920 to_module_entry = get_module_entry(to_module, CHECK);
921 if (to_module_entry == nullptr) {
922 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
923 "to_module is invalid");
924 }
925 } else {
926 to_module_entry = nullptr;
992 assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()),
993 "Class loader is not a subclass of java.lang.ClassLoader");
994 assert(package_name != nullptr, "the package_name should not be null");
995
996 if (strlen(package_name) == 0) {
997 return nullptr;
998 }
999 TempNewSymbol package_sym = SymbolTable::new_symbol(package_name);
1000 const PackageEntry* const pkg_entry =
1001 get_package_entry_by_name(package_sym, h_loader);
1002 const ModuleEntry* const module_entry = (pkg_entry != nullptr ? pkg_entry->module() : nullptr);
1003
1004 if (module_entry != nullptr && module_entry->module() != nullptr && module_entry->is_named()) {
1005 return module_entry->module();
1006 }
1007 return nullptr;
1008 }
1009
1010 // Export package in module to all unnamed modules.
1011 void Modules::add_module_exports_to_all_unnamed(Handle module, jstring package_name, TRAPS) {
1012 check_cds_restrictions(Handle(), module, CHECK);
1013 if (module.is_null()) {
1014 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
1015 "module is null");
1016 }
1017 if (package_name == nullptr) {
1018 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
1019 "package is null");
1020 }
1021 ModuleEntry* module_entry = get_module_entry(module, CHECK);
1022 if (module_entry == nullptr) {
1023 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1024 "module is invalid");
1025 }
1026
1027 // No-op for unnamed module and open modules
1028 if (!module_entry->is_named() || module_entry->is_open())
1029 return;
1030
1031 ResourceMark rm(THREAD);
1032 char buf[128];
|