< prev index next >

src/hotspot/share/classfile/systemDictionary.cpp

Print this page




  51 #include "jfr/jfrEvents.hpp"
  52 #include "logging/log.hpp"
  53 #include "logging/logStream.hpp"
  54 #include "memory/filemap.hpp"
  55 #include "memory/heapShared.hpp"
  56 #include "memory/metaspaceClosure.hpp"
  57 #include "memory/oopFactory.hpp"
  58 #include "memory/resourceArea.hpp"
  59 #include "oops/access.inline.hpp"
  60 #include "oops/instanceKlass.hpp"
  61 #include "oops/instanceRefKlass.hpp"
  62 #include "oops/klass.inline.hpp"
  63 #include "oops/method.inline.hpp"
  64 #include "oops/methodData.hpp"
  65 #include "oops/objArrayKlass.hpp"
  66 #include "oops/objArrayOop.inline.hpp"
  67 #include "oops/oop.inline.hpp"
  68 #include "oops/symbol.hpp"
  69 #include "oops/typeArrayKlass.hpp"
  70 #include "prims/jvmtiExport.hpp"

  71 #include "prims/methodHandles.hpp"
  72 #include "runtime/arguments.hpp"
  73 #include "runtime/biasedLocking.hpp"
  74 #include "runtime/fieldType.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/orderAccess.hpp"
  80 #include "runtime/sharedRuntime.hpp"
  81 #include "runtime/signature.hpp"
  82 #include "services/classLoadingService.hpp"
  83 #include "services/diagnosticCommand.hpp"
  84 #include "services/threadService.hpp"
  85 #include "utilities/macros.hpp"
  86 #if INCLUDE_CDS
  87 #include "classfile/systemDictionaryShared.hpp"
  88 #endif
  89 #if INCLUDE_JVMCI
  90 #include "jvmci/jvmciRuntime.hpp"


 974     // dimension and object_key in FieldArrayInfo are assigned as a
 975     // side-effect of this call
 976     FieldArrayInfo fd;
 977     BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(NULL));
 978     if (t != T_OBJECT) {
 979       k = Universe::typeArrayKlassObj(t);
 980     } else {
 981       k = SystemDictionary::find(fd.object_key(), class_loader, protection_domain, THREAD);
 982     }
 983     if (k != NULL) {
 984       k = k->array_klass_or_null(fd.dimension());
 985     }
 986   } else {
 987     k = find(class_name, class_loader, protection_domain, THREAD);
 988   }
 989   return k;
 990 }
 991 
 992 // Note: this method is much like resolve_from_stream, but
 993 // does not publish the classes via the SystemDictionary.
 994 // Handles unsafe_DefineAnonymousClass and redefineclasses
 995 // RedefinedClasses do not add to the class hierarchy
 996 InstanceKlass* SystemDictionary::parse_stream(Symbol* class_name,
 997                                               Handle class_loader,
 998                                               Handle protection_domain,
 999                                               ClassFileStream* st,
1000                                               const InstanceKlass* unsafe_anonymous_host,
1001                                               GrowableArray<Handle>* cp_patches,





1002                                               TRAPS) {
1003 
1004   EventClassLoad class_load_start_event;
1005 
1006   ClassLoaderData* loader_data;

1007   if (unsafe_anonymous_host != NULL) {
1008     // Create a new CLD for an unsafe anonymous class, that uses the same class loader
1009     // as the unsafe_anonymous_host
1010     guarantee(oopDesc::equals(unsafe_anonymous_host->class_loader(), class_loader()), "should be the same");
1011     loader_data = ClassLoaderData::unsafe_anonymous_class_loader_data(class_loader);










1012   } else {
1013     loader_data = ClassLoaderData::class_loader_data(class_loader());
1014   }
1015 
1016   assert(st != NULL, "invariant");
1017   assert(st->need_verify(), "invariant");
1018 
1019   // Parse stream and create a klass.
1020   // Note that we do this even though this klass might
1021   // already be present in the SystemDictionary, otherwise we would not
1022   // throw potential ClassFormatErrors.
1023 
1024   InstanceKlass* k = KlassFactory::create_from_stream(st,
1025                                                       class_name,
1026                                                       loader_data,
1027                                                       protection_domain,
1028                                                       unsafe_anonymous_host,
1029                                                       cp_patches,




1030                                                       CHECK_NULL);
1031 
1032   if (unsafe_anonymous_host != NULL && k != NULL) {
1033     // Unsafe anonymous classes must update ClassLoaderData holder (was unsafe_anonymous_host loader)
1034     // so that they can be unloaded when the mirror is no longer referenced.
1035     k->class_loader_data()->initialize_holder(Handle(THREAD, k->java_mirror()));


1036 
1037     {
1038       MutexLocker mu_r(Compile_lock, THREAD);
1039 
1040       // Add to class hierarchy, initialize vtables, and do possible
1041       // deoptimizations.
1042       add_to_hierarchy(k, CHECK_NULL); // No exception, but can block
1043 
1044       // But, do not add to dictionary.
1045 
1046       // compiled code dependencies need to be validated anyway
1047       notice_modification();
1048     }
1049 
1050     // Rewrite and patch constant pool here.
1051     k->link_class(CHECK_NULL);
1052     if (cp_patches != NULL) {
1053       k->constants()->patch_resolved_references(cp_patches);
1054     }
1055 
1056     // If it's anonymous, initialize it now, since nobody else will.

1057     k->eager_initialize(CHECK_NULL);
1058 
1059     // notify jvmti
1060     if (JvmtiExport::should_post_class_load()) {
1061         assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1062         JvmtiExport::post_class_load((JavaThread *) THREAD, k);
1063     }
1064     if (class_load_start_event.should_commit()) {
1065       post_class_load_event(&class_load_start_event, k, loader_data);
1066     }
1067   }
1068   assert(unsafe_anonymous_host != NULL || NULL == cp_patches,
1069          "cp_patches only found with unsafe_anonymous_host");
1070 
1071   return k;
1072 }
1073 
1074 // Add a klass to the system from a stream (called by jni_DefineClass and
1075 // JVM_DefineClass).
1076 // Note: class_name can be NULL. In that case we do not know the name of
1077 // the class until we have parsed the stream.
1078 
1079 InstanceKlass* SystemDictionary::resolve_from_stream(Symbol* class_name,
1080                                                      Handle class_loader,
1081                                                      Handle protection_domain,
1082                                                      ClassFileStream* st,

1083                                                      TRAPS) {
1084 
1085   HandleMark hm(THREAD);
1086 
1087   // Classloaders that support parallelism, e.g. bootstrap classloader,
1088   // do not acquire lock here
1089   bool DoObjectLock = true;
1090   if (is_parallelCapable(class_loader)) {
1091     DoObjectLock = false;
1092   }
1093 
1094   ClassLoaderData* loader_data = register_loader(class_loader);
1095 
1096   // Make sure we are synchronized on the class loader before we proceed
1097   Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
1098   check_loader_lock_contention(lockObject, THREAD);
1099   ObjectLocker ol(lockObject, THREAD, DoObjectLock);
1100 
1101   assert(st != NULL, "invariant");
1102 


1107  InstanceKlass* k = NULL;
1108 
1109 #if INCLUDE_CDS
1110   if (!DumpSharedSpaces) {
1111     k = SystemDictionaryShared::lookup_from_stream(class_name,
1112                                                    class_loader,
1113                                                    protection_domain,
1114                                                    st,
1115                                                    CHECK_NULL);
1116   }
1117 #endif
1118 
1119   if (k == NULL) {
1120     if (st->buffer() == NULL) {
1121       return NULL;
1122     }
1123     k = KlassFactory::create_from_stream(st,
1124                                          class_name,
1125                                          loader_data,
1126                                          protection_domain,
1127                                          NULL, // unsafe_anonymous_host
1128                                          NULL, // cp_patches




1129                                          CHECK_NULL);
1130   }
1131 
1132   assert(k != NULL, "no klass created");
1133   Symbol* h_name = k->name();
1134   assert(class_name == NULL || class_name == h_name, "name mismatch");
1135 
1136   // Add class just loaded
1137   // If a class loader supports parallel classloading handle parallel define requests
1138   // find_or_define_instance_class may return a different InstanceKlass
1139   if (is_parallelCapable(class_loader)) {
1140     InstanceKlass* defined_k = find_or_define_instance_class(h_name, class_loader, k, THREAD);
1141     if (!HAS_PENDING_EXCEPTION && defined_k != k) {
1142       // If a parallel capable class loader already defined this class, register 'k' for cleanup.
1143       assert(defined_k != NULL, "Should have a klass if there's no exception");
1144       loader_data->add_to_deallocate_list(k);
1145       k = defined_k;
1146     }
1147   } else {
1148     define_instance_class(k, THREAD);


1818 
1819   bool unloading_occurred;
1820   bool is_concurrent = !SafepointSynchronize::is_at_safepoint();
1821   {
1822     GCTraceTime(Debug, gc, phases) t("ClassLoaderData", gc_timer);
1823     assert_locked_or_safepoint(ClassLoaderDataGraph_lock);  // caller locks.
1824     // First, mark for unload all ClassLoaderData referencing a dead class loader.
1825     unloading_occurred = ClassLoaderDataGraph::do_unloading();
1826     if (unloading_occurred) {
1827       MutexLockerEx ml2(is_concurrent ? Module_lock : NULL);
1828       JFR_ONLY(Jfr::on_unloading_classes();)
1829 
1830       MutexLockerEx ml1(is_concurrent ? SystemDictionary_lock : NULL);
1831       ClassLoaderDataGraph::clean_module_and_package_info();
1832       constraints()->purge_loader_constraints();
1833       resolution_errors()->purge_resolution_errors();
1834     }
1835   }
1836 
1837   GCTraceTime(Debug, gc, phases) t("Trigger cleanups", gc_timer);


1838 
1839   if (unloading_occurred) {
1840     SymbolTable::trigger_cleanup();
1841 
1842     // Oops referenced by the protection domain cache table may get unreachable independently
1843     // of the class loader (eg. cached protection domain oops). So we need to
1844     // explicitly unlink them here.
1845     // All protection domain oops are linked to the caller class, so if nothing
1846     // unloads, this is not needed.
1847     _pd_cache_table->trigger_cleanup();
1848   }
1849 
1850   return unloading_occurred;
1851 }
1852 
1853 void SystemDictionary::oops_do(OopClosure* f) {
1854   f->do_oop(&_java_system_loader);
1855   f->do_oop(&_java_platform_loader);
1856   f->do_oop(&_system_loader_lock_obj);
1857   CDS_ONLY(SystemDictionaryShared::oops_do(f);)




  51 #include "jfr/jfrEvents.hpp"
  52 #include "logging/log.hpp"
  53 #include "logging/logStream.hpp"
  54 #include "memory/filemap.hpp"
  55 #include "memory/heapShared.hpp"
  56 #include "memory/metaspaceClosure.hpp"
  57 #include "memory/oopFactory.hpp"
  58 #include "memory/resourceArea.hpp"
  59 #include "oops/access.inline.hpp"
  60 #include "oops/instanceKlass.hpp"
  61 #include "oops/instanceRefKlass.hpp"
  62 #include "oops/klass.inline.hpp"
  63 #include "oops/method.inline.hpp"
  64 #include "oops/methodData.hpp"
  65 #include "oops/objArrayKlass.hpp"
  66 #include "oops/objArrayOop.inline.hpp"
  67 #include "oops/oop.inline.hpp"
  68 #include "oops/symbol.hpp"
  69 #include "oops/typeArrayKlass.hpp"
  70 #include "prims/jvmtiExport.hpp"
  71 #include "prims/resolvedMethodTable.hpp"
  72 #include "prims/methodHandles.hpp"
  73 #include "runtime/arguments.hpp"
  74 #include "runtime/biasedLocking.hpp"
  75 #include "runtime/fieldType.hpp"
  76 #include "runtime/handles.inline.hpp"
  77 #include "runtime/java.hpp"
  78 #include "runtime/javaCalls.hpp"
  79 #include "runtime/mutexLocker.hpp"
  80 #include "runtime/orderAccess.hpp"
  81 #include "runtime/sharedRuntime.hpp"
  82 #include "runtime/signature.hpp"
  83 #include "services/classLoadingService.hpp"
  84 #include "services/diagnosticCommand.hpp"
  85 #include "services/threadService.hpp"
  86 #include "utilities/macros.hpp"
  87 #if INCLUDE_CDS
  88 #include "classfile/systemDictionaryShared.hpp"
  89 #endif
  90 #if INCLUDE_JVMCI
  91 #include "jvmci/jvmciRuntime.hpp"


 975     // dimension and object_key in FieldArrayInfo are assigned as a
 976     // side-effect of this call
 977     FieldArrayInfo fd;
 978     BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(NULL));
 979     if (t != T_OBJECT) {
 980       k = Universe::typeArrayKlassObj(t);
 981     } else {
 982       k = SystemDictionary::find(fd.object_key(), class_loader, protection_domain, THREAD);
 983     }
 984     if (k != NULL) {
 985       k = k->array_klass_or_null(fd.dimension());
 986     }
 987   } else {
 988     k = find(class_name, class_loader, protection_domain, THREAD);
 989   }
 990   return k;
 991 }
 992 
 993 // Note: this method is much like resolve_from_stream, but
 994 // does not publish the classes via the SystemDictionary.
 995 // Handles Lookup.defineClass nonfindable, unsafe_DefineAnonymousClass
 996 // and redefineclasses. RedefinedClasses do not add to the class hierarchy.
 997 InstanceKlass* SystemDictionary::parse_stream(Symbol* class_name,
 998                                               Handle class_loader,
 999                                               Handle protection_domain,
1000                                               ClassFileStream* st,
1001                                               const InstanceKlass* unsafe_anonymous_host,
1002                                               GrowableArray<Handle>* cp_patches,
1003                                               const bool is_nonfindable,
1004                                               const bool is_weaknonfindable,
1005                                               const bool can_access_vm_annotations,
1006                                               InstanceKlass* dynamic_nest_host,
1007                                               Handle classData,
1008                                               TRAPS) {
1009 
1010   EventClassLoad class_load_start_event;
1011 
1012   ClassLoaderData* loader_data;
1013 
1014   if (unsafe_anonymous_host != NULL) {
1015     // - for unsafe anonymous class: create a new short-lived CLD that uses the same
1016     //                               class loader as the unsafe_anonymous_host.
1017     guarantee(oopDesc::equals(unsafe_anonymous_host->class_loader(), class_loader()), "should be the same");
1018     loader_data = ClassLoaderData::shortlived_class_loader_data(class_loader);
1019   } else if (is_nonfindable) {
1020     // - for weak nonfindable class: create a new short-lived CLD whose loader is
1021     //                               the Lookup class' loader.
1022     // - for nonfindable class: add the class to the Lookup class' loader's CLD.
1023     if (is_weaknonfindable) {
1024       loader_data = ClassLoaderData::shortlived_class_loader_data(class_loader);
1025     } else {
1026       // This nonfindable class goes into the regular CLD pool for this loader.
1027       loader_data = register_loader(class_loader);
1028     }
1029   } else {
1030     loader_data = ClassLoaderData::class_loader_data(class_loader());
1031   }
1032 
1033   assert(st != NULL, "invariant");
1034   assert(st->need_verify(), "invariant");
1035 
1036   // Parse stream and create a klass.
1037   // Note that we do this even though this klass might
1038   // already be present in the SystemDictionary, otherwise we would not
1039   // throw potential ClassFormatErrors.
1040 
1041   InstanceKlass* k = KlassFactory::create_from_stream(st,
1042                                                       class_name,
1043                                                       loader_data,
1044                                                       protection_domain,
1045                                                       unsafe_anonymous_host,
1046                                                       cp_patches,
1047                                                       is_nonfindable,
1048                                                       can_access_vm_annotations,
1049                                                       dynamic_nest_host,
1050                                                       classData,
1051                                                       CHECK_NULL);
1052 
1053   if ((is_nonfindable || (unsafe_anonymous_host != NULL)) && k != NULL) {
1054     // Weak nonfindable and unsafe anonymous classes must update ClassLoaderData holder
1055     // so that they can be unloaded when the mirror is no longer referenced.
1056     if (is_weaknonfindable || (unsafe_anonymous_host != NULL)) {
1057       k->class_loader_data()->initialize_holder(Handle(THREAD, k->java_mirror()));
1058     }
1059 
1060     {
1061       MutexLocker mu_r(Compile_lock, THREAD);
1062 
1063       // Add to class hierarchy, initialize vtables, and do possible
1064       // deoptimizations.
1065       add_to_hierarchy(k, CHECK_NULL); // No exception, but can block
1066 
1067       // But, do not add to dictionary.
1068 
1069       // compiled code dependencies need to be validated anyway
1070       notice_modification();
1071     }
1072 
1073     // Rewrite and patch constant pool here.
1074     k->link_class(CHECK_NULL);
1075     if (cp_patches != NULL) {
1076       k->constants()->patch_resolved_references(cp_patches);
1077     }
1078 
1079     // Initialize it now, since nobody else will.
1080     // FIXME: why must we eager initialize? It should be initialized upon use.
1081     k->eager_initialize(CHECK_NULL);
1082 
1083     // notify jvmti
1084     if (JvmtiExport::should_post_class_load()) {
1085         assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1086         JvmtiExport::post_class_load((JavaThread *) THREAD, k);
1087     }
1088     if (class_load_start_event.should_commit()) {
1089       post_class_load_event(&class_load_start_event, k, loader_data);
1090     }
1091   }
1092   assert(unsafe_anonymous_host != NULL || NULL == cp_patches,
1093          "cp_patches only found with unsafe_anonymous_host");
1094 
1095   return k;
1096 }
1097 
1098 // Add a klass to the system from a stream (called by jni_DefineClass and
1099 // JVM_DefineClass).
1100 // Note: class_name can be NULL. In that case we do not know the name of
1101 // the class until we have parsed the stream.
1102 
1103 InstanceKlass* SystemDictionary::resolve_from_stream(Symbol* class_name,
1104                                                      Handle class_loader,
1105                                                      Handle protection_domain,
1106                                                      ClassFileStream* st,
1107                                                      InstanceKlass* dynamic_nest_host,
1108                                                      TRAPS) {
1109 
1110   HandleMark hm(THREAD);
1111 
1112   // Classloaders that support parallelism, e.g. bootstrap classloader,
1113   // do not acquire lock here
1114   bool DoObjectLock = true;
1115   if (is_parallelCapable(class_loader)) {
1116     DoObjectLock = false;
1117   }
1118 
1119   ClassLoaderData* loader_data = register_loader(class_loader);
1120 
1121   // Make sure we are synchronized on the class loader before we proceed
1122   Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
1123   check_loader_lock_contention(lockObject, THREAD);
1124   ObjectLocker ol(lockObject, THREAD, DoObjectLock);
1125 
1126   assert(st != NULL, "invariant");
1127 


1132  InstanceKlass* k = NULL;
1133 
1134 #if INCLUDE_CDS
1135   if (!DumpSharedSpaces) {
1136     k = SystemDictionaryShared::lookup_from_stream(class_name,
1137                                                    class_loader,
1138                                                    protection_domain,
1139                                                    st,
1140                                                    CHECK_NULL);
1141   }
1142 #endif
1143 
1144   if (k == NULL) {
1145     if (st->buffer() == NULL) {
1146       return NULL;
1147     }
1148     k = KlassFactory::create_from_stream(st,
1149                                          class_name,
1150                                          loader_data,
1151                                          protection_domain,
1152                                          NULL,  // unsafe_anonymous_host
1153                                          NULL,  // cp_patches
1154                                          false, // is_nonfindable
1155                                          false, // can_access_vm_annotations
1156                                          dynamic_nest_host,
1157                                          Handle(), // classData
1158                                          CHECK_NULL);
1159   }
1160 
1161   assert(k != NULL, "no klass created");
1162   Symbol* h_name = k->name();
1163   assert(class_name == NULL || class_name == h_name, "name mismatch");
1164 
1165   // Add class just loaded
1166   // If a class loader supports parallel classloading handle parallel define requests
1167   // find_or_define_instance_class may return a different InstanceKlass
1168   if (is_parallelCapable(class_loader)) {
1169     InstanceKlass* defined_k = find_or_define_instance_class(h_name, class_loader, k, THREAD);
1170     if (!HAS_PENDING_EXCEPTION && defined_k != k) {
1171       // If a parallel capable class loader already defined this class, register 'k' for cleanup.
1172       assert(defined_k != NULL, "Should have a klass if there's no exception");
1173       loader_data->add_to_deallocate_list(k);
1174       k = defined_k;
1175     }
1176   } else {
1177     define_instance_class(k, THREAD);


1847 
1848   bool unloading_occurred;
1849   bool is_concurrent = !SafepointSynchronize::is_at_safepoint();
1850   {
1851     GCTraceTime(Debug, gc, phases) t("ClassLoaderData", gc_timer);
1852     assert_locked_or_safepoint(ClassLoaderDataGraph_lock);  // caller locks.
1853     // First, mark for unload all ClassLoaderData referencing a dead class loader.
1854     unloading_occurred = ClassLoaderDataGraph::do_unloading();
1855     if (unloading_occurred) {
1856       MutexLockerEx ml2(is_concurrent ? Module_lock : NULL);
1857       JFR_ONLY(Jfr::on_unloading_classes();)
1858 
1859       MutexLockerEx ml1(is_concurrent ? SystemDictionary_lock : NULL);
1860       ClassLoaderDataGraph::clean_module_and_package_info();
1861       constraints()->purge_loader_constraints();
1862       resolution_errors()->purge_resolution_errors();
1863     }
1864   }
1865 
1866   GCTraceTime(Debug, gc, phases) t("Trigger cleanups", gc_timer);
1867   // Trigger cleaning the ResolvedMethodTable even if no unloading occurred.
1868   ResolvedMethodTable::trigger_cleanup();
1869 
1870   if (unloading_occurred) {
1871     SymbolTable::trigger_cleanup();
1872 
1873     // Oops referenced by the protection domain cache table may get unreachable independently
1874     // of the class loader (eg. cached protection domain oops). So we need to
1875     // explicitly unlink them here.
1876     // All protection domain oops are linked to the caller class, so if nothing
1877     // unloads, this is not needed.
1878     _pd_cache_table->trigger_cleanup();
1879   }
1880 
1881   return unloading_occurred;
1882 }
1883 
1884 void SystemDictionary::oops_do(OopClosure* f) {
1885   f->do_oop(&_java_system_loader);
1886   f->do_oop(&_java_platform_loader);
1887   f->do_oop(&_system_loader_lock_obj);
1888   CDS_ONLY(SystemDictionaryShared::oops_do(f);)


< prev index next >