38 #include "classfile/loaderConstraints.hpp"
39 #include "classfile/packageEntry.hpp"
40 #include "classfile/placeholders.hpp"
41 #include "classfile/resolutionErrors.hpp"
42 #include "classfile/stringTable.hpp"
43 #include "classfile/symbolTable.hpp"
44 #include "classfile/systemDictionary.hpp"
45 #include "classfile/vmClasses.hpp"
46 #include "classfile/vmSymbols.hpp"
47 #include "gc/shared/gcTraceTime.inline.hpp"
48 #include "interpreter/bootstrapInfo.hpp"
49 #include "jfr/jfrEvents.hpp"
50 #include "jvm.h"
51 #include "logging/log.hpp"
52 #include "logging/logStream.hpp"
53 #include "memory/metaspaceClosure.hpp"
54 #include "memory/oopFactory.hpp"
55 #include "memory/resourceArea.hpp"
56 #include "memory/universe.hpp"
57 #include "oops/access.inline.hpp"
58 #include "oops/instanceKlass.hpp"
59 #include "oops/klass.inline.hpp"
60 #include "oops/method.inline.hpp"
61 #include "oops/objArrayKlass.hpp"
62 #include "oops/objArrayOop.inline.hpp"
63 #include "oops/oop.inline.hpp"
64 #include "oops/oop.hpp"
65 #include "oops/oopHandle.hpp"
66 #include "oops/oopHandle.inline.hpp"
67 #include "oops/symbol.hpp"
68 #include "oops/typeArrayKlass.hpp"
69 #include "prims/jvmtiExport.hpp"
70 #include "prims/methodHandles.hpp"
71 #include "runtime/arguments.hpp"
72 #include "runtime/atomic.hpp"
73 #include "runtime/handles.inline.hpp"
74 #include "runtime/java.hpp"
75 #include "runtime/javaCalls.hpp"
76 #include "runtime/mutexLocker.hpp"
77 #include "runtime/sharedRuntime.hpp"
78 #include "runtime/signature.hpp"
79 #include "runtime/synchronizer.hpp"
80 #include "services/classLoadingService.hpp"
81 #include "services/diagnosticCommand.hpp"
82 #include "services/finalizerService.hpp"
83 #include "services/threadService.hpp"
84 #include "utilities/growableArray.hpp"
85 #include "utilities/macros.hpp"
86 #include "utilities/utf8.hpp"
87 #if INCLUDE_CDS
88 #include "classfile/systemDictionaryShared.hpp"
89 #endif
90 #if INCLUDE_JFR
91 #include "jfr/jfr.hpp"
92 #endif
93
94 class InvokeMethodKey : public StackObj {
95 private:
96 Symbol* _symbol;
332
333 Klass* SystemDictionary::resolve_or_fail(Symbol* class_name, Handle class_loader,
334 bool throw_error, TRAPS) {
335 Klass* klass = resolve_or_null(class_name, class_loader, THREAD);
336 // Check for pending exception or null klass, and throw exception
337 if (HAS_PENDING_EXCEPTION || klass == nullptr) {
338 handle_resolution_exception(class_name, throw_error, CHECK_NULL);
339 }
340 return klass;
341 }
342
343 // Forwards to resolve_array_class_or_null or resolve_instance_class_or_null
344
345 Klass* SystemDictionary::resolve_or_null(Symbol* class_name, Handle class_loader, TRAPS) {
346 if (Signature::is_array(class_name)) {
347 return resolve_array_class_or_null(class_name, class_loader, THREAD);
348 } else {
349 assert(class_name != nullptr && !Signature::is_array(class_name), "must be");
350 if (Signature::has_envelope(class_name)) {
351 ResourceMark rm(THREAD);
352 // Ignore wrapping L and ;.
353 TempNewSymbol name = SymbolTable::new_symbol(class_name->as_C_string() + 1,
354 class_name->utf8_length() - 2);
355 return resolve_instance_class_or_null(name, class_loader, THREAD);
356 } else {
357 return resolve_instance_class_or_null(class_name, class_loader, THREAD);
358 }
359 }
360 }
361
362 // Forwards to resolve_instance_class_or_null
363
364 Klass* SystemDictionary::resolve_array_class_or_null(Symbol* class_name,
365 Handle class_loader,
366 TRAPS) {
367 assert(Signature::is_array(class_name), "must be array");
368 ResourceMark rm(THREAD);
369 SignatureStream ss(class_name, false);
370 int ndims = ss.skip_array_prefix(); // skip all '['s
371 Klass* k = nullptr;
372 BasicType t = ss.type();
901
902 InstanceKlass* SystemDictionary::resolve_from_stream(ClassFileStream* st,
903 Symbol* class_name,
904 Handle class_loader,
905 const ClassLoadInfo& cl_info,
906 TRAPS) {
907 if (cl_info.is_hidden()) {
908 return resolve_hidden_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
909 } else {
910 return resolve_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
911 }
912 }
913
914
915 #if INCLUDE_CDS
916 // Check if a shared class can be loaded by the specific classloader.
917 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
918 InstanceKlass* ik,
919 PackageEntry* pkg_entry,
920 Handle class_loader) {
921 assert(!ModuleEntryTable::javabase_moduleEntry()->is_patched(),
922 "Cannot use sharing if java.base is patched");
923
924 // (1) Check if we are loading into the same loader as in dump time.
925
926 if (ik->is_shared_boot_class()) {
927 if (class_loader() != nullptr) {
928 return false;
929 }
930 } else if (ik->is_shared_platform_class()) {
931 if (class_loader() != java_platform_loader()) {
932 return false;
933 }
934 } else if (ik->is_shared_app_class()) {
935 if (class_loader() != java_system_loader()) {
936 return false;
937 }
938 } else {
939 // ik was loaded by a custom loader during dump time
940 if (class_loader_data(class_loader)->is_builtin_class_loader_data()) {
941 return false;
942 } else {
978
979 if (pkg_entry == nullptr) {
980 // We might have looked up pkg_entry before the module system was initialized.
981 // Need to reload it now.
982 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
983 if (pkg_name != nullptr) {
984 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
985 }
986 }
987
988 ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
989 bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
990 bool was_archived_from_named_module = !cl->has_unnamed_module();
991 bool visible;
992
993 if (was_archived_from_named_module) {
994 if (should_be_in_named_module) {
995 // Is the module loaded from the same location as during dump time?
996 visible = mod_entry->shared_path_index() == scp_index;
997 if (visible) {
998 assert(!mod_entry->is_patched(), "cannot load archived classes for patched module");
999 }
1000 } else {
1001 // During dump time, this class was in a named module, but at run time, this class should be
1002 // in an unnamed module.
1003 visible = false;
1004 }
1005 } else {
1006 if (should_be_in_named_module) {
1007 // During dump time, this class was in an unnamed, but at run time, this class should be
1008 // in a named module.
1009 visible = false;
1010 } else {
1011 visible = true;
1012 }
1013 }
1014
1015 return visible;
1016 }
1017
1018 bool SystemDictionary::check_shared_class_super_type(InstanceKlass* klass, InstanceKlass* super_type,
1110 Handle protection_domain,
1111 const ClassFileStream *cfs,
1112 PackageEntry* pkg_entry,
1113 TRAPS) {
1114 assert(ik != nullptr, "sanity");
1115 assert(!ik->is_unshareable_info_restored(), "shared class can be restored only once");
1116 assert(Atomic::add(&ik->_shared_class_load_count, 1) == 1, "shared class loaded more than once");
1117 Symbol* class_name = ik->name();
1118
1119 if (!is_shared_class_visible(class_name, ik, pkg_entry, class_loader)) {
1120 ik->set_shared_loading_failed();
1121 return nullptr;
1122 }
1123
1124 bool check = check_shared_class_super_types(ik, class_loader, CHECK_NULL);
1125 if (!check) {
1126 ik->set_shared_loading_failed();
1127 return nullptr;
1128 }
1129
1130 InstanceKlass* new_ik = nullptr;
1131 // CFLH check is skipped for VM hidden classes (see KlassFactory::create_from_stream).
1132 // It will be skipped for shared VM hidden lambda proxy classes.
1133 if (!SystemDictionaryShared::is_hidden_lambda_proxy(ik)) {
1134 new_ik = KlassFactory::check_shared_class_file_load_hook(
1135 ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1136 }
1137 if (new_ik != nullptr) {
1138 // The class is changed by CFLH. Return the new class. The shared class is
1139 // not used.
1140 return new_ik;
1141 }
1142
1143 // Adjust methods to recover missing data. They need addresses for
1144 // interpreter entry points and their default native method address
1145 // must be reset.
1146
1147 // Shared classes are all currently loaded by either the bootstrap or
1148 // internal parallel class loaders, so this will never cause a deadlock
1149 // on a custom class loader lock.
1150 // Since this class is already locked with parallel capable class
1151 // loaders, including the bootstrap loader via the placeholder table,
1152 // this lock is currently a nop.
1153
1154 ClassLoaderData* loader_data = class_loader_data(class_loader);
1155 {
1156 HandleMark hm(THREAD);
1157 Handle lockObject = get_loader_lock_or_null(class_loader);
1158 ObjectLocker ol(lockObject, THREAD);
1159 // prohibited package check assumes all classes loaded from archive call
1160 // restore_unshareable_info which calls ik->set_package()
1161 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1162 }
1163
1164 load_shared_class_misc(ik, loader_data);
1165 return ik;
1166 }
1167
1168 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1169 ik->print_class_load_logging(loader_data, nullptr, nullptr);
1170
1171 // For boot loader, ensure that GetSystemPackage knows that a class in this
1172 // package was loaded.
1173 if (loader_data->is_the_null_class_loader_data()) {
1174 s2 path_index = ik->shared_classpath_index();
1175 ik->set_classpath_index(path_index);
1176 }
1177
1178 // notify a class loaded from shared object
1179 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1180
1181 if (CDSConfig::is_dumping_final_static_archive()) {
1182 SystemDictionaryShared::init_dumptime_info_from_preimage(ik);
1183 }
1184 }
|
38 #include "classfile/loaderConstraints.hpp"
39 #include "classfile/packageEntry.hpp"
40 #include "classfile/placeholders.hpp"
41 #include "classfile/resolutionErrors.hpp"
42 #include "classfile/stringTable.hpp"
43 #include "classfile/symbolTable.hpp"
44 #include "classfile/systemDictionary.hpp"
45 #include "classfile/vmClasses.hpp"
46 #include "classfile/vmSymbols.hpp"
47 #include "gc/shared/gcTraceTime.inline.hpp"
48 #include "interpreter/bootstrapInfo.hpp"
49 #include "jfr/jfrEvents.hpp"
50 #include "jvm.h"
51 #include "logging/log.hpp"
52 #include "logging/logStream.hpp"
53 #include "memory/metaspaceClosure.hpp"
54 #include "memory/oopFactory.hpp"
55 #include "memory/resourceArea.hpp"
56 #include "memory/universe.hpp"
57 #include "oops/access.inline.hpp"
58 #include "oops/fieldStreams.inline.hpp"
59 #include "oops/instanceKlass.hpp"
60 #include "oops/klass.inline.hpp"
61 #include "oops/method.inline.hpp"
62 #include "oops/objArrayKlass.hpp"
63 #include "oops/objArrayOop.inline.hpp"
64 #include "oops/oop.inline.hpp"
65 #include "oops/oop.hpp"
66 #include "oops/oopHandle.hpp"
67 #include "oops/oopHandle.inline.hpp"
68 #include "oops/symbol.hpp"
69 #include "oops/typeArrayKlass.hpp"
70 #include "oops/inlineKlass.inline.hpp"
71 #include "prims/jvmtiExport.hpp"
72 #include "prims/methodHandles.hpp"
73 #include "runtime/arguments.hpp"
74 #include "runtime/atomic.hpp"
75 #include "runtime/handles.inline.hpp"
76 #include "runtime/java.hpp"
77 #include "runtime/javaCalls.hpp"
78 #include "runtime/mutexLocker.hpp"
79 #include "runtime/os.hpp"
80 #include "runtime/sharedRuntime.hpp"
81 #include "runtime/signature.hpp"
82 #include "runtime/synchronizer.hpp"
83 #include "services/classLoadingService.hpp"
84 #include "services/diagnosticCommand.hpp"
85 #include "services/finalizerService.hpp"
86 #include "services/threadService.hpp"
87 #include "utilities/growableArray.hpp"
88 #include "utilities/macros.hpp"
89 #include "utilities/utf8.hpp"
90 #if INCLUDE_CDS
91 #include "classfile/systemDictionaryShared.hpp"
92 #endif
93 #if INCLUDE_JFR
94 #include "jfr/jfr.hpp"
95 #endif
96
97 class InvokeMethodKey : public StackObj {
98 private:
99 Symbol* _symbol;
335
336 Klass* SystemDictionary::resolve_or_fail(Symbol* class_name, Handle class_loader,
337 bool throw_error, TRAPS) {
338 Klass* klass = resolve_or_null(class_name, class_loader, THREAD);
339 // Check for pending exception or null klass, and throw exception
340 if (HAS_PENDING_EXCEPTION || klass == nullptr) {
341 handle_resolution_exception(class_name, throw_error, CHECK_NULL);
342 }
343 return klass;
344 }
345
346 // Forwards to resolve_array_class_or_null or resolve_instance_class_or_null
347
348 Klass* SystemDictionary::resolve_or_null(Symbol* class_name, Handle class_loader, TRAPS) {
349 if (Signature::is_array(class_name)) {
350 return resolve_array_class_or_null(class_name, class_loader, THREAD);
351 } else {
352 assert(class_name != nullptr && !Signature::is_array(class_name), "must be");
353 if (Signature::has_envelope(class_name)) {
354 ResourceMark rm(THREAD);
355 // Ignore wrapping L and ; (and Q and ; for value types).
356 TempNewSymbol name = SymbolTable::new_symbol(class_name->as_C_string() + 1,
357 class_name->utf8_length() - 2);
358 return resolve_instance_class_or_null(name, class_loader, THREAD);
359 } else {
360 return resolve_instance_class_or_null(class_name, class_loader, THREAD);
361 }
362 }
363 }
364
365 // Forwards to resolve_instance_class_or_null
366
367 Klass* SystemDictionary::resolve_array_class_or_null(Symbol* class_name,
368 Handle class_loader,
369 TRAPS) {
370 assert(Signature::is_array(class_name), "must be array");
371 ResourceMark rm(THREAD);
372 SignatureStream ss(class_name, false);
373 int ndims = ss.skip_array_prefix(); // skip all '['s
374 Klass* k = nullptr;
375 BasicType t = ss.type();
904
905 InstanceKlass* SystemDictionary::resolve_from_stream(ClassFileStream* st,
906 Symbol* class_name,
907 Handle class_loader,
908 const ClassLoadInfo& cl_info,
909 TRAPS) {
910 if (cl_info.is_hidden()) {
911 return resolve_hidden_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
912 } else {
913 return resolve_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
914 }
915 }
916
917
918 #if INCLUDE_CDS
919 // Check if a shared class can be loaded by the specific classloader.
920 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
921 InstanceKlass* ik,
922 PackageEntry* pkg_entry,
923 Handle class_loader) {
924 assert(!CDSConfig::module_patching_disables_cds(), "Cannot use CDS");
925
926 // (1) Check if we are loading into the same loader as in dump time.
927
928 if (ik->is_shared_boot_class()) {
929 if (class_loader() != nullptr) {
930 return false;
931 }
932 } else if (ik->is_shared_platform_class()) {
933 if (class_loader() != java_platform_loader()) {
934 return false;
935 }
936 } else if (ik->is_shared_app_class()) {
937 if (class_loader() != java_system_loader()) {
938 return false;
939 }
940 } else {
941 // ik was loaded by a custom loader during dump time
942 if (class_loader_data(class_loader)->is_builtin_class_loader_data()) {
943 return false;
944 } else {
980
981 if (pkg_entry == nullptr) {
982 // We might have looked up pkg_entry before the module system was initialized.
983 // Need to reload it now.
984 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
985 if (pkg_name != nullptr) {
986 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
987 }
988 }
989
990 ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
991 bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
992 bool was_archived_from_named_module = !cl->has_unnamed_module();
993 bool visible;
994
995 if (was_archived_from_named_module) {
996 if (should_be_in_named_module) {
997 // Is the module loaded from the same location as during dump time?
998 visible = mod_entry->shared_path_index() == scp_index;
999 if (visible) {
1000 assert(!CDSConfig::module_patching_disables_cds(), "Cannot use CDS");
1001 }
1002 } else {
1003 // During dump time, this class was in a named module, but at run time, this class should be
1004 // in an unnamed module.
1005 visible = false;
1006 }
1007 } else {
1008 if (should_be_in_named_module) {
1009 // During dump time, this class was in an unnamed, but at run time, this class should be
1010 // in a named module.
1011 visible = false;
1012 } else {
1013 visible = true;
1014 }
1015 }
1016
1017 return visible;
1018 }
1019
1020 bool SystemDictionary::check_shared_class_super_type(InstanceKlass* klass, InstanceKlass* super_type,
1112 Handle protection_domain,
1113 const ClassFileStream *cfs,
1114 PackageEntry* pkg_entry,
1115 TRAPS) {
1116 assert(ik != nullptr, "sanity");
1117 assert(!ik->is_unshareable_info_restored(), "shared class can be restored only once");
1118 assert(Atomic::add(&ik->_shared_class_load_count, 1) == 1, "shared class loaded more than once");
1119 Symbol* class_name = ik->name();
1120
1121 if (!is_shared_class_visible(class_name, ik, pkg_entry, class_loader)) {
1122 ik->set_shared_loading_failed();
1123 return nullptr;
1124 }
1125
1126 bool check = check_shared_class_super_types(ik, class_loader, CHECK_NULL);
1127 if (!check) {
1128 ik->set_shared_loading_failed();
1129 return nullptr;
1130 }
1131
1132 if (ik->has_inline_type_fields()) {
1133 for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
1134 if (fs.access_flags().is_static()) continue;
1135 Symbol* sig = fs.signature();
1136 if (fs.is_null_free_inline_type()) {
1137 // Pre-load inline class
1138 TempNewSymbol name = Signature::strip_envelope(sig);
1139 Klass* real_k = SystemDictionary::resolve_with_circularity_detection_or_fail(ik->name(), name,
1140 class_loader, false, CHECK_NULL);
1141 Klass* k = ik->get_inline_type_field_klass_or_null(fs.index());
1142 if (real_k != k) {
1143 // oops, the app has substituted a different version of k!
1144 return nullptr;
1145 }
1146 } else if (Signature::has_envelope(sig)) {
1147 TempNewSymbol name = Signature::strip_envelope(sig);
1148 if (name != ik->name() && ik->is_class_in_loadable_descriptors_attribute(name)) {
1149 Klass* real_k = SystemDictionary::resolve_with_circularity_detection_or_fail(ik->name(), name,
1150 class_loader, false, THREAD);
1151 if (HAS_PENDING_EXCEPTION) {
1152 CLEAR_PENDING_EXCEPTION;
1153 }
1154 Klass* k = ik->get_inline_type_field_klass_or_null(fs.index());
1155 if (real_k != k) {
1156 // oops, the app has substituted a different version of k!
1157 return nullptr;
1158 }
1159 }
1160 }
1161 }
1162 }
1163
1164 InstanceKlass* new_ik = nullptr;
1165 // CFLH check is skipped for VM hidden classes (see KlassFactory::create_from_stream).
1166 // It will be skipped for shared VM hidden lambda proxy classes.
1167 if (!SystemDictionaryShared::is_hidden_lambda_proxy(ik)) {
1168 new_ik = KlassFactory::check_shared_class_file_load_hook(
1169 ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1170 }
1171 if (new_ik != nullptr) {
1172 // The class is changed by CFLH. Return the new class. The shared class is
1173 // not used.
1174 return new_ik;
1175 }
1176
1177 // Adjust methods to recover missing data. They need addresses for
1178 // interpreter entry points and their default native method address
1179 // must be reset.
1180
1181 // Shared classes are all currently loaded by either the bootstrap or
1182 // internal parallel class loaders, so this will never cause a deadlock
1183 // on a custom class loader lock.
1184 // Since this class is already locked with parallel capable class
1185 // loaders, including the bootstrap loader via the placeholder table,
1186 // this lock is currently a nop.
1187
1188 ClassLoaderData* loader_data = class_loader_data(class_loader);
1189 {
1190 HandleMark hm(THREAD);
1191 Handle lockObject = get_loader_lock_or_null(class_loader);
1192 ObjectLocker ol(lockObject, THREAD);
1193 // prohibited package check assumes all classes loaded from archive call
1194 // restore_unshareable_info which calls ik->set_package()
1195 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1196 }
1197
1198 load_shared_class_misc(ik, loader_data);
1199
1200 return ik;
1201 }
1202
1203 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1204 ik->print_class_load_logging(loader_data, nullptr, nullptr);
1205
1206 // For boot loader, ensure that GetSystemPackage knows that a class in this
1207 // package was loaded.
1208 if (loader_data->is_the_null_class_loader_data()) {
1209 s2 path_index = ik->shared_classpath_index();
1210 ik->set_classpath_index(path_index);
1211 }
1212
1213 // notify a class loaded from shared object
1214 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1215
1216 if (CDSConfig::is_dumping_final_static_archive()) {
1217 SystemDictionaryShared::init_dumptime_info_from_preimage(ik);
1218 }
1219 }
|