< prev index next >

src/share/vm/classfile/systemDictionary.cpp

Print this page




  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoaderData.inline.hpp"
  27 #include "classfile/dictionary.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "classfile/loaderConstraints.hpp"
  30 #include "classfile/placeholders.hpp"
  31 #include "classfile/resolutionErrors.hpp"
  32 #include "classfile/systemDictionary.hpp"
  33 #if INCLUDE_CDS
  34 #include "classfile/sharedClassUtil.hpp"
  35 #include "classfile/systemDictionaryShared.hpp"
  36 #endif
  37 #include "classfile/vmSymbols.hpp"
  38 #include "compiler/compileBroker.hpp"
  39 #include "interpreter/bytecodeStream.hpp"
  40 #include "interpreter/interpreter.hpp"


  41 #include "memory/filemap.hpp"
  42 #include "memory/gcLocker.hpp"
  43 #include "memory/oopFactory.hpp"
  44 #include "oops/instanceKlass.hpp"
  45 #include "oops/instanceRefKlass.hpp"
  46 #include "oops/klass.inline.hpp"
  47 #include "oops/methodData.hpp"
  48 #include "oops/objArrayKlass.hpp"
  49 #include "oops/oop.inline.hpp"
  50 #include "oops/oop.inline2.hpp"
  51 #include "oops/typeArrayKlass.hpp"
  52 #include "prims/jvmtiEnvBase.hpp"
  53 #include "prims/methodHandles.hpp"
  54 #include "runtime/arguments.hpp"
  55 #include "runtime/biasedLocking.hpp"
  56 #include "runtime/fieldType.hpp"
  57 #include "runtime/handles.inline.hpp"
  58 #include "runtime/java.hpp"
  59 #include "runtime/javaCalls.hpp"
  60 #include "runtime/mutexLocker.hpp"
  61 #include "runtime/orderAccess.inline.hpp"
  62 #include "runtime/signature.hpp"
  63 #include "services/classLoadingService.hpp"
  64 #include "services/threadService.hpp"
  65 #include "utilities/macros.hpp"
  66 #include "utilities/ticks.hpp"
  67 #if INCLUDE_TRACE
  68 #include "trace/tracing.hpp"
  69 #endif
  70 
  71 Dictionary*            SystemDictionary::_dictionary          = NULL;
  72 PlaceholderTable*      SystemDictionary::_placeholders        = NULL;
  73 Dictionary*            SystemDictionary::_shared_dictionary   = NULL;
  74 LoaderConstraintTable* SystemDictionary::_loader_constraints  = NULL;
  75 ResolutionErrorTable*  SystemDictionary::_resolution_errors   = NULL;
  76 SymbolPropertyTable*   SystemDictionary::_invoke_method_table = NULL;
  77 
  78 
  79 int         SystemDictionary::_number_of_modifications = 0;
  80 int         SystemDictionary::_sdgeneration               = 0;
  81 const int   SystemDictionary::_primelist[_prime_array_size] = {1009,2017,4049,5051,10103,
  82               20201,40423,99991};
  83 
  84 oop         SystemDictionary::_system_loader_lock_obj     =  NULL;
  85 
  86 Klass*      SystemDictionary::_well_known_klasses[SystemDictionary::WKID_LIMIT]
  87                                                           =  { NULL /*, NULL...*/ };
  88 
  89 Klass*      SystemDictionary::_box_klasses[T_VOID+1]      =  { NULL /*, NULL...*/ };
  90 
  91 oop         SystemDictionary::_java_system_loader         =  NULL;
  92 
  93 bool        SystemDictionary::_has_loadClassInternal      =  false;
  94 bool        SystemDictionary::_has_checkPackageAccess     =  false;
  95 
  96 // lazily initialized klass variables
  97 Klass* volatile SystemDictionary::_abstract_ownable_synchronizer_klass = NULL;
  98 



  99 
 100 // ----------------------------------------------------------------------------
 101 // Java-level SystemLoader
 102 
 103 oop SystemDictionary::java_system_loader() {
 104   return _java_system_loader;
 105 }
 106 
 107 void SystemDictionary::compute_java_system_loader(TRAPS) {
 108   KlassHandle system_klass(THREAD, WK_KLASS(ClassLoader_klass));
 109   JavaValue result(T_OBJECT);
 110   JavaCalls::call_static(&result,
 111                          KlassHandle(THREAD, WK_KLASS(ClassLoader_klass)),
 112                          vmSymbols::getSystemClassLoader_name(),
 113                          vmSymbols::void_classloader_signature(),
 114                          CHECK);
 115 
 116   _java_system_loader = (oop)result.get_jobject();
 117 
 118   CDS_ONLY(SystemDictionaryShared::initialize(CHECK);)


 124   return ClassLoaderDataGraph::find_or_create(class_loader, THREAD);
 125 }
 126 
 127 // ----------------------------------------------------------------------------
 128 // debugging
 129 
 130 #ifdef ASSERT
 131 
 132 // return true if class_name contains no '.' (internal format is '/')
 133 bool SystemDictionary::is_internal_format(Symbol* class_name) {
 134   if (class_name != NULL) {
 135     ResourceMark rm;
 136     char* name = class_name->as_C_string();
 137     return strchr(name, '.') == NULL;
 138   } else {
 139     return true;
 140   }
 141 }
 142 
 143 #endif



 144 
 145 // ----------------------------------------------------------------------------
 146 // Parallel class loading check
 147 
 148 bool SystemDictionary::is_parallelCapable(Handle class_loader) {
 149   if (UnsyncloadClass || class_loader.is_null()) return true;
 150   if (AlwaysLockClassLoader) return false;
 151   return java_lang_ClassLoader::parallelCapable(class_loader());
 152 }
 153 // ----------------------------------------------------------------------------
 154 // ParallelDefineClass flag does not apply to bootclass loader
 155 bool SystemDictionary::is_parallelDefine(Handle class_loader) {
 156    if (class_loader.is_null()) return false;
 157    if (AllowParallelDefineClass && java_lang_ClassLoader::parallelCapable(class_loader())) {
 158      return true;
 159    }
 160    return false;
 161 }
 162 
 163 /**


 581         // This logic has the current thread wait once it has done
 582         // all the superclass/superinterface loading it can, until
 583         // the original thread completes the class loading or fails
 584         // If it completes we will use the resulting InstanceKlass
 585         // which we will find below in the systemDictionary.
 586         // We also get here for parallel bootstrap classloader
 587         if (class_loader.is_null()) {
 588           SystemDictionary_lock->wait();
 589         } else {
 590           double_lock_wait(lockObject, THREAD);
 591         }
 592       } else {
 593         // If not in SD and not in PH, other thread's load must have failed
 594         super_load_in_progress = false;
 595       }
 596     }
 597   }
 598   return (nh);
 599 }
 600 
















 601 
 602 Klass* SystemDictionary::resolve_instance_class_or_null(Symbol* name,
 603                                                         Handle class_loader,
 604                                                         Handle protection_domain,
 605                                                         TRAPS) {
 606   assert(name != NULL && !FieldType::is_array(name) &&
 607          !FieldType::is_obj(name), "invalid class name");
 608 
 609   Ticks class_load_start_time = Ticks::now();
 610 
 611   // UseNewReflection
 612   // Fix for 4474172; see evaluation for more details
 613   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
 614   ClassLoaderData *loader_data = register_loader(class_loader, CHECK_NULL);
 615 
 616   // Do lookup to see if class already exist and the protection domain
 617   // has the right access
 618   // This call uses find which checks protection domain already matches
 619   // All subsequent calls use find_class, and set has_loaded_class so that
 620   // before we return a result we call out to java to check for valid protection domain
 621   // to allow returning the Klass* and add it to the pd_set if it is valid
 622   unsigned int d_hash = dictionary()->compute_hash(name, loader_data);
 623   int d_index = dictionary()->hash_to_index(d_hash);
 624   Klass* probe = dictionary()->find(d_index, d_hash, name, loader_data,
 625                                       protection_domain, THREAD);
 626   if (probe != NULL) return probe;
 627 
 628 
 629   // Non-bootstrap class loaders will call out to class loader and


 840             JvmtiExport::post_class_load((JavaThread *) thread, k());
 841           }
 842         }
 843       }
 844     } // load_instance_class loop
 845 
 846     if (load_instance_added == true) {
 847       // clean up placeholder entries for LOAD_INSTANCE success or error
 848       // This brackets the SystemDictionary updates for both defining
 849       // and initiating loaders
 850       MutexLocker mu(SystemDictionary_lock, THREAD);
 851       placeholders()->find_and_remove(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, THREAD);
 852       SystemDictionary_lock->notify_all();
 853     }
 854   }
 855 
 856   if (HAS_PENDING_EXCEPTION || k.is_null()) {
 857     return NULL;
 858   }
 859 
 860   post_class_load_event(class_load_start_time, k, class_loader);
 861 
 862 #ifdef ASSERT
 863   {
 864     ClassLoaderData* loader_data = k->class_loader_data();
 865     MutexLocker mu(SystemDictionary_lock, THREAD);
 866     Klass* kk = find_class(name, loader_data);
 867     assert(kk == k(), "should be present in dictionary");
 868   }
 869 #endif
 870 
 871   // return if the protection domain in NULL
 872   if (protection_domain() == NULL) return k();
 873 
 874   // Check the protection domain has the right access
 875   {
 876     MutexLocker mu(SystemDictionary_lock, THREAD);
 877     // Note that we have an entry, and entries can be deleted only during GC,
 878     // so we cannot allow GC to occur while we're holding this entry.
 879     // We're using a No_Safepoint_Verifier to catch any place where we
 880     // might potentially do a GC at all.


 965       k = k->array_klass_or_null(fd.dimension());
 966     }
 967   } else {
 968     k = find(class_name, class_loader, protection_domain, THREAD);
 969   }
 970   return k;
 971 }
 972 
 973 // Note: this method is much like resolve_from_stream, but
 974 // updates no supplemental data structures.
 975 // TODO consolidate the two methods with a helper routine?
 976 Klass* SystemDictionary::parse_stream(Symbol* class_name,
 977                                       Handle class_loader,
 978                                       Handle protection_domain,
 979                                       ClassFileStream* st,
 980                                       KlassHandle host_klass,
 981                                       GrowableArray<Handle>* cp_patches,
 982                                       TRAPS) {
 983   TempNewSymbol parsed_name = NULL;
 984 
 985   Ticks class_load_start_time = Ticks::now();
 986 
 987   ClassLoaderData* loader_data;
 988   if (host_klass.not_null()) {
 989     // Create a new CLD for anonymous class, that uses the same class loader
 990     // as the host_klass
 991     assert(EnableInvokeDynamic, "");
 992     guarantee(host_klass->class_loader() == class_loader(), "should be the same");
 993     guarantee(!DumpSharedSpaces, "must not create anonymous classes when dumping");
 994     loader_data = ClassLoaderData::anonymous_class_loader_data(class_loader(), CHECK_NULL);
 995     loader_data->record_dependency(host_klass(), CHECK_NULL);
 996   } else {
 997     loader_data = ClassLoaderData::class_loader_data(class_loader());
 998   }
 999 
1000   // Parse the stream. Note that we do this even though this klass might
1001   // already be present in the SystemDictionary, otherwise we would not
1002   // throw potential ClassFormatErrors.
1003   //
1004   // Note: "name" is updated.
1005 


1026 
1027       // But, do not add to system dictionary.
1028 
1029       // compiled code dependencies need to be validated anyway
1030       notice_modification();
1031     }
1032 
1033     // Rewrite and patch constant pool here.
1034     k->link_class(CHECK_NULL);
1035     if (cp_patches != NULL) {
1036       k->constants()->patch_resolved_references(cp_patches);
1037     }
1038     k->eager_initialize(CHECK_NULL);
1039 
1040     // notify jvmti
1041     if (JvmtiExport::should_post_class_load()) {
1042         assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1043         JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1044     }
1045 
1046     post_class_load_event(class_load_start_time, k, class_loader);
1047   }
1048   assert(host_klass.not_null() || cp_patches == NULL,
1049          "cp_patches only found with host_klass");
1050 
1051   return k();
1052 }
1053 
1054 // Add a klass to the system from a stream (called by jni_DefineClass and
1055 // JVM_DefineClass).
1056 // Note: class_name can be NULL. In that case we do not know the name of
1057 // the class until we have parsed the stream.
1058 
1059 Klass* SystemDictionary::resolve_from_stream(Symbol* class_name,
1060                                              Handle class_loader,
1061                                              Handle protection_domain,
1062                                              ClassFileStream* st,
1063                                              bool verify,
1064                                              TRAPS) {
1065 
1066   // Classloaders that support parallelism, e.g. bootstrap classloader,


1068   bool DoObjectLock = true;
1069   if (is_parallelCapable(class_loader)) {
1070     DoObjectLock = false;
1071   }
1072 
1073   ClassLoaderData* loader_data = register_loader(class_loader, CHECK_NULL);
1074 
1075   // Make sure we are synchronized on the class loader before we proceed
1076   Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
1077   check_loader_lock_contention(lockObject, THREAD);
1078   ObjectLocker ol(lockObject, THREAD, DoObjectLock);
1079 
1080   TempNewSymbol parsed_name = NULL;
1081 
1082   // Parse the stream. Note that we do this even though this klass might
1083   // already be present in the SystemDictionary, otherwise we would not
1084   // throw potential ClassFormatErrors.
1085   //
1086   // Note: "name" is updated.
1087 
1088   instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name,
1089                                                              loader_data,
1090                                                              protection_domain,
1091                                                              parsed_name,
1092                                                              verify,
1093                                                              THREAD);

1094 
1095   const char* pkg = "java/";
1096   size_t pkglen = strlen(pkg);
1097   if (!HAS_PENDING_EXCEPTION &&
1098       !class_loader.is_null() &&
1099       parsed_name != NULL &&
1100       parsed_name->utf8_length() >= (int)pkglen &&
1101       !strncmp((const char*)parsed_name->bytes(), pkg, pkglen)) {
1102     // It is illegal to define classes in the "java." package from
1103     // JVM_DefineClass or jni_DefineClass unless you're the bootclassloader
1104     ResourceMark rm(THREAD);
1105     char* name = parsed_name->as_C_string();
1106     char* index = strrchr(name, '/');
1107     assert(index != NULL, "must be");
1108     *index = '\0'; // chop to just the package name
1109     while ((index = strchr(name, '/')) != NULL) {
1110       *index = '.'; // replace '/' with '.' in package name
1111     }
1112     const char* fmt = "Prohibited package name: %s";
1113     size_t len = strlen(fmt) + strlen(name);
1114     char* message = NEW_RESOURCE_ARRAY(char, len);
1115     jio_snprintf(message, len, fmt, name);
1116     Exceptions::_throw_msg(THREAD_AND_LOCATION,
1117       vmSymbols::java_lang_SecurityException(), message);
1118   }
1119 
1120   if (!HAS_PENDING_EXCEPTION) {
1121     assert(parsed_name != NULL, "Sanity");
1122     assert(class_name == NULL || class_name == parsed_name, "name mismatch");
1123     // Verification prevents us from creating names with dots in them, this
1124     // asserts that that's the case.
1125     assert(is_internal_format(parsed_name),
1126            "external class name format used internally");
1127 








1128     // Add class just loaded
1129     // If a class loader supports parallel classloading handle parallel define requests
1130     // find_or_define_instance_class may return a different InstanceKlass
1131     if (is_parallelCapable(class_loader)) {
1132       k = find_or_define_instance_class(class_name, class_loader, k, THREAD);
1133     } else {
1134       define_instance_class(k, THREAD);
1135     }
1136   }
1137 
1138   // Make sure we have an entry in the SystemDictionary on success
1139   debug_only( {
1140     if (!HAS_PENDING_EXCEPTION) {
1141       assert(parsed_name != NULL, "parsed_name is still null?");
1142       Symbol*  h_name    = k->name();
1143       ClassLoaderData *defining_loader_data = k->class_loader_data();
1144 
1145       MutexLocker mu(SystemDictionary_lock, THREAD);
1146 
1147       Klass* check = find_class(parsed_name, loader_data);


1290     // Search the shared system dictionary for classes preloaded into the
1291     // shared spaces.
1292     instanceKlassHandle k;
1293     {
1294 #if INCLUDE_CDS
1295       PerfTraceTime vmtimer(ClassLoader::perf_shared_classload_time());
1296       k = load_shared_class(class_name, class_loader, THREAD);
1297 #endif
1298     }
1299 
1300     if (k.is_null()) {
1301       // Use VM class loader
1302       PerfTraceTime vmtimer(ClassLoader::perf_sys_classload_time());
1303       k = ClassLoader::load_classfile(class_name, CHECK_(nh));
1304     }
1305 
1306     // find_or_define_instance_class may return a different InstanceKlass
1307     if (!k.is_null()) {
1308       k = find_or_define_instance_class(class_name, class_loader, k, CHECK_(nh));
1309     }


















1310     return k;
1311   } else {
1312     // Use user specified class loader to load class. Call loadClass operation on class_loader.
1313     ResourceMark rm(THREAD);
1314 
1315     assert(THREAD->is_Java_thread(), "must be a JavaThread");
1316     JavaThread* jt = (JavaThread*) THREAD;
1317 
1318     PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
1319                                ClassLoader::perf_app_classload_selftime(),
1320                                ClassLoader::perf_app_classload_count(),
1321                                jt->get_thread_stat()->perf_recursion_counts_addr(),
1322                                jt->get_thread_stat()->perf_timers_addr(),
1323                                PerfClassTraceTime::CLASS_LOAD);
1324 
1325     Handle s = java_lang_String::create_from_symbol(class_name, CHECK_(nh));
1326     // Translate to external class name format, i.e., convert '/' chars to '.'
1327     Handle string = java_lang_String::externalize_classname(s, CHECK_(nh));
1328 
1329     JavaValue result(T_OBJECT);


1368     assert(result.get_type() == T_OBJECT, "just checking");
1369     oop obj = (oop) result.get_jobject();
1370 
1371     // Primitive classes return null since forName() can not be
1372     // used to obtain any of the Class objects representing primitives or void
1373     if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
1374       instanceKlassHandle k =
1375                 instanceKlassHandle(THREAD, java_lang_Class::as_Klass(obj));
1376       // For user defined Java class loaders, check that the name returned is
1377       // the same as that requested.  This check is done for the bootstrap
1378       // loader when parsing the class file.
1379       if (class_name == k->name()) {
1380         return k;
1381       }
1382     }
1383     // Class is not found or has the wrong name, return NULL
1384     return nh;
1385   }
1386 }
1387 









1388 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) {
1389 
1390   ClassLoaderData* loader_data = k->class_loader_data();
1391   Handle class_loader_h(THREAD, loader_data->class_loader());
1392 
1393   for (uintx it = 0; it < GCExpandToAllocateDelayMillis; it++){}
1394 
1395  // for bootstrap and other parallel classloaders don't acquire lock,
1396  // use placeholder token
1397  // If a parallelCapable class loader calls define_instance_class instead of
1398  // find_or_define_instance_class to get here, we have a timing
1399  // hole with systemDictionary updates and check_constraints
1400  if (!class_loader_h.is_null() && !is_parallelCapable(class_loader_h)) {
1401     assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD,
1402          compute_loader_lock_object(class_loader_h, THREAD)),
1403          "define called without lock");
1404   }
1405 
1406   // Check class-loading constraints. Throw exception if violation is detected.
1407   // Grabs and releases SystemDictionary_lock


1438     MutexLocker mu_r(Compile_lock, THREAD);
1439 
1440     // Add to class hierarchy, initialize vtables, and do possible
1441     // deoptimizations.
1442     add_to_hierarchy(k, CHECK); // No exception, but can block
1443 
1444     // Add to systemDictionary - so other classes can see it.
1445     // Grabs and releases SystemDictionary_lock
1446     update_dictionary(d_index, d_hash, p_index, p_hash,
1447                       k, class_loader_h, THREAD);
1448   }
1449   k->eager_initialize(THREAD);
1450 
1451   // notify jvmti
1452   if (JvmtiExport::should_post_class_load()) {
1453       assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1454       JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1455 
1456   }
1457 

1458 }
1459 
1460 // Support parallel classloading
1461 // All parallel class loaders, including bootstrap classloader
1462 // lock a placeholder entry for this class/class_loader pair
1463 // to allow parallel defines of different classes for this class loader
1464 // With AllowParallelDefine flag==true, in case they do not synchronize around
1465 // FindLoadedClass/DefineClass, calls, we check for parallel
1466 // loading for them, wait if a defineClass is in progress
1467 // and return the initial requestor's results
1468 // This flag does not apply to the bootstrap classloader.
1469 // With AllowParallelDefine flag==false, call through to define_instance_class
1470 // which will throw LinkageError: duplicate class definition.
1471 // False is the requested default.
1472 // For better performance, the class loaders should synchronize
1473 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they
1474 // potentially waste time reading and parsing the bytestream.
1475 // Note: VM callers should ensure consistency of k/class_name,class_loader
1476 instanceKlassHandle SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader, instanceKlassHandle k, TRAPS) {
1477 


1699 
1700   template <class T> void do_oop_work(T* p) {
1701     oop obj = oopDesc::load_decode_heap_oop(p);
1702     guarantee(_is_alive->do_object_b(obj), "Oop in system dictionary must be live");
1703   }
1704 
1705 public:
1706   VerifySDReachableAndLiveClosure(BoolObjectClosure* is_alive) : OopClosure(), _is_alive(is_alive) { }
1707 
1708   virtual void do_oop(oop* p)       { do_oop_work(p); }
1709   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
1710 };
1711 #endif
1712 
1713 // Assumes classes in the SystemDictionary are only unloaded at a safepoint
1714 // Note: anonymous classes are not in the SD.
1715 bool SystemDictionary::do_unloading(BoolObjectClosure* is_alive, bool clean_alive) {
1716   // First, mark for unload all ClassLoaderData referencing a dead class loader.
1717   bool unloading_occurred = ClassLoaderDataGraph::do_unloading(is_alive, clean_alive);
1718   if (unloading_occurred) {

1719     dictionary()->do_unloading();
1720     constraints()->purge_loader_constraints();
1721     resolution_errors()->purge_resolution_errors();
1722   }
1723   // Oops referenced by the system dictionary may get unreachable independently
1724   // of the class loader (eg. cached protection domain oops). So we need to
1725   // explicitly unlink them here instead of in Dictionary::do_unloading.
1726   dictionary()->unlink(is_alive);
1727 #ifdef ASSERT
1728   VerifySDReachableAndLiveClosure cl(is_alive);
1729   dictionary()->oops_do(&cl);
1730 #endif
1731   return unloading_occurred;
1732 }
1733 
1734 void SystemDictionary::roots_oops_do(OopClosure* strong, OopClosure* weak) {
1735   strong->do_oop(&_java_system_loader);
1736   strong->do_oop(&_system_loader_lock_obj);
1737   CDS_ONLY(SystemDictionaryShared::roots_oops_do(strong);)
1738 


1832 
1833 // ----------------------------------------------------------------------------
1834 // Initialization
1835 
1836 void SystemDictionary::initialize(TRAPS) {
1837   // Allocate arrays
1838   assert(dictionary() == NULL,
1839          "SystemDictionary should only be initialized once");
1840   _sdgeneration        = 0;
1841   _dictionary          = new Dictionary(calculate_systemdictionary_size(PredictedLoadedClassCount));
1842   _placeholders        = new PlaceholderTable(_nof_buckets);
1843   _number_of_modifications = 0;
1844   _loader_constraints  = new LoaderConstraintTable(_loader_constraint_size);
1845   _resolution_errors   = new ResolutionErrorTable(_resolution_error_size);
1846   _invoke_method_table = new SymbolPropertyTable(_invoke_method_size);
1847 
1848   // Allocate private object used as system class loader lock
1849   _system_loader_lock_obj = oopFactory::new_intArray(0, CHECK);
1850   // Initialize basic classes
1851   initialize_preloaded_classes(CHECK);



1852 }
1853 
1854 // Compact table of directions on the initialization of klasses:
1855 static const short wk_init_info[] = {
1856   #define WK_KLASS_INIT_INFO(name, symbol, option) \
1857     ( ((int)vmSymbols::VM_SYMBOL_ENUM_NAME(symbol) \
1858           << SystemDictionary::CEIL_LG_OPTION_LIMIT) \
1859       | (int)SystemDictionary::option ),
1860   WK_KLASSES_DO(WK_KLASS_INIT_INFO)
1861   #undef WK_KLASS_INIT_INFO
1862   0
1863 };
1864 
1865 bool SystemDictionary::initialize_wk_klass(WKID id, int init_opt, TRAPS) {
1866   assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
1867   int  info = wk_init_info[id - FIRST_WKID];
1868   int  sid  = (info >> CEIL_LG_OPTION_LIMIT);
1869   Symbol* symbol = vmSymbols::symbol_at((vmSymbols::SID)sid);
1870   Klass**    klassp = &_well_known_klasses[id];
1871   bool must_load = (init_opt < SystemDictionary::Opt);


2668 }
2669 
2670 
2671 void SystemDictionary::verify() {
2672   guarantee(dictionary() != NULL, "Verify of system dictionary failed");
2673   guarantee(constraints() != NULL,
2674             "Verify of loader constraints failed");
2675   guarantee(dictionary()->number_of_entries() >= 0 &&
2676             placeholders()->number_of_entries() >= 0,
2677             "Verify of system dictionary failed");
2678 
2679   // Verify dictionary
2680   dictionary()->verify();
2681 
2682   GCMutexLocker mu(SystemDictionary_lock);
2683   placeholders()->verify();
2684 
2685   // Verify constraint table
2686   guarantee(constraints() != NULL, "Verify of loader constraints failed");
2687   constraints()->verify(dictionary(), placeholders());
2688 }
2689 
2690 // utility function for class load event
2691 void SystemDictionary::post_class_load_event(const Ticks& start_time,
2692                                              instanceKlassHandle k,
2693                                              Handle initiating_loader) {
2694 #if INCLUDE_TRACE
2695   EventClassLoad event(UNTIMED);
2696   if (event.should_commit()) {
2697     event.set_starttime(start_time);
2698     event.set_loadedClass(k());
2699     oop defining_class_loader = k->class_loader();
2700     event.set_definingClassLoader(defining_class_loader !=  NULL ?
2701                                     defining_class_loader->klass() : (Klass*)NULL);
2702     oop class_loader = initiating_loader.is_null() ? (oop)NULL : initiating_loader();
2703     event.set_initiatingClassLoader(class_loader != NULL ?
2704                                       class_loader->klass() : (Klass*)NULL);
2705     event.commit();
2706   }
2707 #endif // INCLUDE_TRACE
2708 }
2709 
2710 #ifndef PRODUCT
2711 
2712 // statistics code
2713 class ClassStatistics: AllStatic {
2714  private:
2715   static int nclasses;        // number of classes
2716   static int nmethods;        // number of methods
2717   static int nmethoddata;     // number of methodData
2718   static int class_size;      // size of class objects in words
2719   static int method_size;     // size of method objects in words
2720   static int debug_size;      // size of debug info in methods
2721   static int methoddata_size; // size of methodData objects in words
2722 
2723   static void do_class(Klass* k) {
2724     nclasses++;
2725     class_size += k->size();
2726     if (k->oop_is_instance()) {
2727       InstanceKlass* ik = (InstanceKlass*)k;




  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoaderData.inline.hpp"
  27 #include "classfile/dictionary.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "classfile/loaderConstraints.hpp"
  30 #include "classfile/placeholders.hpp"
  31 #include "classfile/resolutionErrors.hpp"
  32 #include "classfile/systemDictionary.hpp"
  33 #if INCLUDE_CDS
  34 #include "classfile/sharedClassUtil.hpp"
  35 #include "classfile/systemDictionaryShared.hpp"
  36 #endif
  37 #include "classfile/vmSymbols.hpp"
  38 #include "compiler/compileBroker.hpp"
  39 #include "interpreter/bytecodeStream.hpp"
  40 #include "interpreter/interpreter.hpp"
  41 #include "jfr/jfrEvents.hpp"
  42 #include "jfr/jni/jfrUpcalls.hpp"
  43 #include "memory/filemap.hpp"
  44 #include "memory/gcLocker.hpp"
  45 #include "memory/oopFactory.hpp"
  46 #include "oops/instanceKlass.hpp"
  47 #include "oops/instanceRefKlass.hpp"
  48 #include "oops/klass.inline.hpp"
  49 #include "oops/methodData.hpp"
  50 #include "oops/objArrayKlass.hpp"
  51 #include "oops/oop.inline.hpp"
  52 #include "oops/oop.inline2.hpp"
  53 #include "oops/typeArrayKlass.hpp"
  54 #include "prims/jvmtiEnvBase.hpp"
  55 #include "prims/methodHandles.hpp"
  56 #include "runtime/arguments.hpp"
  57 #include "runtime/biasedLocking.hpp"
  58 #include "runtime/fieldType.hpp"
  59 #include "runtime/handles.inline.hpp"
  60 #include "runtime/java.hpp"
  61 #include "runtime/javaCalls.hpp"
  62 #include "runtime/mutexLocker.hpp"
  63 #include "runtime/orderAccess.inline.hpp"
  64 #include "runtime/signature.hpp"
  65 #include "services/classLoadingService.hpp"
  66 #include "services/threadService.hpp"
  67 #include "utilities/macros.hpp"
  68 #include "utilities/ticks.hpp"



  69 
  70 Dictionary*            SystemDictionary::_dictionary          = NULL;
  71 PlaceholderTable*      SystemDictionary::_placeholders        = NULL;
  72 Dictionary*            SystemDictionary::_shared_dictionary   = NULL;
  73 LoaderConstraintTable* SystemDictionary::_loader_constraints  = NULL;
  74 ResolutionErrorTable*  SystemDictionary::_resolution_errors   = NULL;
  75 SymbolPropertyTable*   SystemDictionary::_invoke_method_table = NULL;
  76 
  77 
  78 int         SystemDictionary::_number_of_modifications = 0;
  79 int         SystemDictionary::_sdgeneration               = 0;
  80 const int   SystemDictionary::_primelist[_prime_array_size] = {1009,2017,4049,5051,10103,
  81               20201,40423,99991};
  82 
  83 oop         SystemDictionary::_system_loader_lock_obj     =  NULL;
  84 
  85 Klass*      SystemDictionary::_well_known_klasses[SystemDictionary::WKID_LIMIT]
  86                                                           =  { NULL /*, NULL...*/ };
  87 
  88 Klass*      SystemDictionary::_box_klasses[T_VOID+1]      =  { NULL /*, NULL...*/ };
  89 
  90 oop         SystemDictionary::_java_system_loader         =  NULL;
  91 
  92 bool        SystemDictionary::_has_loadClassInternal      =  false;
  93 bool        SystemDictionary::_has_checkPackageAccess     =  false;
  94 
  95 // lazily initialized klass variables
  96 Klass* volatile SystemDictionary::_abstract_ownable_synchronizer_klass = NULL;
  97 
  98 #if INCLUDE_JFR
  99 static const Symbol* jfr_event_handler_proxy = NULL;
 100 #endif // INCLUDE_JFR
 101 
 102 // ----------------------------------------------------------------------------
 103 // Java-level SystemLoader
 104 
 105 oop SystemDictionary::java_system_loader() {
 106   return _java_system_loader;
 107 }
 108 
 109 void SystemDictionary::compute_java_system_loader(TRAPS) {
 110   KlassHandle system_klass(THREAD, WK_KLASS(ClassLoader_klass));
 111   JavaValue result(T_OBJECT);
 112   JavaCalls::call_static(&result,
 113                          KlassHandle(THREAD, WK_KLASS(ClassLoader_klass)),
 114                          vmSymbols::getSystemClassLoader_name(),
 115                          vmSymbols::void_classloader_signature(),
 116                          CHECK);
 117 
 118   _java_system_loader = (oop)result.get_jobject();
 119 
 120   CDS_ONLY(SystemDictionaryShared::initialize(CHECK);)


 126   return ClassLoaderDataGraph::find_or_create(class_loader, THREAD);
 127 }
 128 
 129 // ----------------------------------------------------------------------------
 130 // debugging
 131 
 132 #ifdef ASSERT
 133 
 134 // return true if class_name contains no '.' (internal format is '/')
 135 bool SystemDictionary::is_internal_format(Symbol* class_name) {
 136   if (class_name != NULL) {
 137     ResourceMark rm;
 138     char* name = class_name->as_C_string();
 139     return strchr(name, '.') == NULL;
 140   } else {
 141     return true;
 142   }
 143 }
 144 
 145 #endif
 146 #if INCLUDE_JFR
 147 #include "jfr/jfr.hpp"
 148 #endif
 149 
 150 // ----------------------------------------------------------------------------
 151 // Parallel class loading check
 152 
 153 bool SystemDictionary::is_parallelCapable(Handle class_loader) {
 154   if (UnsyncloadClass || class_loader.is_null()) return true;
 155   if (AlwaysLockClassLoader) return false;
 156   return java_lang_ClassLoader::parallelCapable(class_loader());
 157 }
 158 // ----------------------------------------------------------------------------
 159 // ParallelDefineClass flag does not apply to bootclass loader
 160 bool SystemDictionary::is_parallelDefine(Handle class_loader) {
 161    if (class_loader.is_null()) return false;
 162    if (AllowParallelDefineClass && java_lang_ClassLoader::parallelCapable(class_loader())) {
 163      return true;
 164    }
 165    return false;
 166 }
 167 
 168 /**


 586         // This logic has the current thread wait once it has done
 587         // all the superclass/superinterface loading it can, until
 588         // the original thread completes the class loading or fails
 589         // If it completes we will use the resulting InstanceKlass
 590         // which we will find below in the systemDictionary.
 591         // We also get here for parallel bootstrap classloader
 592         if (class_loader.is_null()) {
 593           SystemDictionary_lock->wait();
 594         } else {
 595           double_lock_wait(lockObject, THREAD);
 596         }
 597       } else {
 598         // If not in SD and not in PH, other thread's load must have failed
 599         super_load_in_progress = false;
 600       }
 601     }
 602   }
 603   return (nh);
 604 }
 605 
 606 // utility function for class load event
 607 static void post_class_load_event(EventClassLoad &event,
 608                                   instanceKlassHandle k,
 609                                   Handle initiating_loader) {
 610 #if INCLUDE_JFR
 611   if (event.should_commit()) {
 612     event.set_loadedClass(k());
 613     event.set_definingClassLoader(k->class_loader_data());
 614     oop class_loader = initiating_loader.is_null() ? (oop)NULL : initiating_loader();
 615     event.set_initiatingClassLoader(class_loader != NULL ?
 616                                     ClassLoaderData::class_loader_data_or_null(class_loader) :
 617                                     (ClassLoaderData*)NULL);
 618     event.commit();
 619   }
 620 #endif
 621 }
 622 
 623 Klass* SystemDictionary::resolve_instance_class_or_null(Symbol* name,
 624                                                         Handle class_loader,
 625                                                         Handle protection_domain,
 626                                                         TRAPS) {
 627   assert(name != NULL && !FieldType::is_array(name) &&
 628          !FieldType::is_obj(name), "invalid class name");
 629 
 630   EventClassLoad class_load_start_event;
 631 
 632   // UseNewReflection
 633   // Fix for 4474172; see evaluation for more details
 634   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
 635   ClassLoaderData *loader_data = register_loader(class_loader, CHECK_NULL);
 636 
 637   // Do lookup to see if class already exist and the protection domain
 638   // has the right access
 639   // This call uses find which checks protection domain already matches
 640   // All subsequent calls use find_class, and set has_loaded_class so that
 641   // before we return a result we call out to java to check for valid protection domain
 642   // to allow returning the Klass* and add it to the pd_set if it is valid
 643   unsigned int d_hash = dictionary()->compute_hash(name, loader_data);
 644   int d_index = dictionary()->hash_to_index(d_hash);
 645   Klass* probe = dictionary()->find(d_index, d_hash, name, loader_data,
 646                                       protection_domain, THREAD);
 647   if (probe != NULL) return probe;
 648 
 649 
 650   // Non-bootstrap class loaders will call out to class loader and


 861             JvmtiExport::post_class_load((JavaThread *) thread, k());
 862           }
 863         }
 864       }
 865     } // load_instance_class loop
 866 
 867     if (load_instance_added == true) {
 868       // clean up placeholder entries for LOAD_INSTANCE success or error
 869       // This brackets the SystemDictionary updates for both defining
 870       // and initiating loaders
 871       MutexLocker mu(SystemDictionary_lock, THREAD);
 872       placeholders()->find_and_remove(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, THREAD);
 873       SystemDictionary_lock->notify_all();
 874     }
 875   }
 876 
 877   if (HAS_PENDING_EXCEPTION || k.is_null()) {
 878     return NULL;
 879   }
 880 
 881   post_class_load_event(class_load_start_event, k, class_loader);
 882 
 883 #ifdef ASSERT
 884   {
 885     ClassLoaderData* loader_data = k->class_loader_data();
 886     MutexLocker mu(SystemDictionary_lock, THREAD);
 887     Klass* kk = find_class(name, loader_data);
 888     assert(kk == k(), "should be present in dictionary");
 889   }
 890 #endif
 891 
 892   // return if the protection domain in NULL
 893   if (protection_domain() == NULL) return k();
 894 
 895   // Check the protection domain has the right access
 896   {
 897     MutexLocker mu(SystemDictionary_lock, THREAD);
 898     // Note that we have an entry, and entries can be deleted only during GC,
 899     // so we cannot allow GC to occur while we're holding this entry.
 900     // We're using a No_Safepoint_Verifier to catch any place where we
 901     // might potentially do a GC at all.


 986       k = k->array_klass_or_null(fd.dimension());
 987     }
 988   } else {
 989     k = find(class_name, class_loader, protection_domain, THREAD);
 990   }
 991   return k;
 992 }
 993 
 994 // Note: this method is much like resolve_from_stream, but
 995 // updates no supplemental data structures.
 996 // TODO consolidate the two methods with a helper routine?
 997 Klass* SystemDictionary::parse_stream(Symbol* class_name,
 998                                       Handle class_loader,
 999                                       Handle protection_domain,
1000                                       ClassFileStream* st,
1001                                       KlassHandle host_klass,
1002                                       GrowableArray<Handle>* cp_patches,
1003                                       TRAPS) {
1004   TempNewSymbol parsed_name = NULL;
1005 
1006   EventClassLoad class_load_start_event;
1007 
1008   ClassLoaderData* loader_data;
1009   if (host_klass.not_null()) {
1010     // Create a new CLD for anonymous class, that uses the same class loader
1011     // as the host_klass
1012     assert(EnableInvokeDynamic, "");
1013     guarantee(host_klass->class_loader() == class_loader(), "should be the same");
1014     guarantee(!DumpSharedSpaces, "must not create anonymous classes when dumping");
1015     loader_data = ClassLoaderData::anonymous_class_loader_data(class_loader(), CHECK_NULL);
1016     loader_data->record_dependency(host_klass(), CHECK_NULL);
1017   } else {
1018     loader_data = ClassLoaderData::class_loader_data(class_loader());
1019   }
1020 
1021   // Parse the stream. Note that we do this even though this klass might
1022   // already be present in the SystemDictionary, otherwise we would not
1023   // throw potential ClassFormatErrors.
1024   //
1025   // Note: "name" is updated.
1026 


1047 
1048       // But, do not add to system dictionary.
1049 
1050       // compiled code dependencies need to be validated anyway
1051       notice_modification();
1052     }
1053 
1054     // Rewrite and patch constant pool here.
1055     k->link_class(CHECK_NULL);
1056     if (cp_patches != NULL) {
1057       k->constants()->patch_resolved_references(cp_patches);
1058     }
1059     k->eager_initialize(CHECK_NULL);
1060 
1061     // notify jvmti
1062     if (JvmtiExport::should_post_class_load()) {
1063         assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1064         JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1065     }
1066 
1067     post_class_load_event(class_load_start_event, k, class_loader);
1068   }
1069   assert(host_klass.not_null() || cp_patches == NULL,
1070          "cp_patches only found with host_klass");
1071 
1072   return k();
1073 }
1074 
1075 // Add a klass to the system from a stream (called by jni_DefineClass and
1076 // JVM_DefineClass).
1077 // Note: class_name can be NULL. In that case we do not know the name of
1078 // the class until we have parsed the stream.
1079 
1080 Klass* SystemDictionary::resolve_from_stream(Symbol* class_name,
1081                                              Handle class_loader,
1082                                              Handle protection_domain,
1083                                              ClassFileStream* st,
1084                                              bool verify,
1085                                              TRAPS) {
1086 
1087   // Classloaders that support parallelism, e.g. bootstrap classloader,


1089   bool DoObjectLock = true;
1090   if (is_parallelCapable(class_loader)) {
1091     DoObjectLock = false;
1092   }
1093 
1094   ClassLoaderData* loader_data = register_loader(class_loader, CHECK_NULL);
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   TempNewSymbol parsed_name = NULL;
1102 
1103   // Parse the stream. Note that we do this even though this klass might
1104   // already be present in the SystemDictionary, otherwise we would not
1105   // throw potential ClassFormatErrors.
1106   //
1107   // Note: "name" is updated.
1108 
1109   ClassFileParser parser(st);
1110   instanceKlassHandle k = parser.parseClassFile(class_name,
1111                                                 loader_data,
1112                                                 protection_domain,
1113                                                 parsed_name,
1114                                                 verify,
1115                                                 THREAD);
1116 
1117   const char* pkg = "java/";
1118   size_t pkglen = strlen(pkg);
1119   if (!HAS_PENDING_EXCEPTION &&
1120       !class_loader.is_null() &&
1121       parsed_name != NULL &&
1122       parsed_name->utf8_length() >= (int)pkglen &&
1123       !strncmp((const char*)parsed_name->bytes(), pkg, pkglen)) {
1124     // It is illegal to define classes in the "java." package from
1125     // JVM_DefineClass or jni_DefineClass unless you're the bootclassloader
1126     ResourceMark rm(THREAD);
1127     char* name = parsed_name->as_C_string();
1128     char* index = strrchr(name, '/');
1129     assert(index != NULL, "must be");
1130     *index = '\0'; // chop to just the package name
1131     while ((index = strchr(name, '/')) != NULL) {
1132       *index = '.'; // replace '/' with '.' in package name
1133     }
1134     const char* fmt = "Prohibited package name: %s";
1135     size_t len = strlen(fmt) + strlen(name);
1136     char* message = NEW_RESOURCE_ARRAY(char, len);
1137     jio_snprintf(message, len, fmt, name);
1138     Exceptions::_throw_msg(THREAD_AND_LOCATION,
1139       vmSymbols::java_lang_SecurityException(), message);
1140   }
1141 
1142   if (!HAS_PENDING_EXCEPTION) {
1143     assert(parsed_name != NULL, "Sanity");
1144     assert(class_name == NULL || class_name == parsed_name, "name mismatch");
1145     // Verification prevents us from creating names with dots in them, this
1146     // asserts that that's the case.
1147     assert(is_internal_format(parsed_name),
1148            "external class name format used internally");
1149 
1150 #if INCLUDE_JFR
1151     {
1152       InstanceKlass* ik = k();
1153       ON_KLASS_CREATION(ik, parser, THREAD);
1154       k = instanceKlassHandle(ik);
1155     }
1156 #endif
1157 
1158     // Add class just loaded
1159     // If a class loader supports parallel classloading handle parallel define requests
1160     // find_or_define_instance_class may return a different InstanceKlass
1161     if (is_parallelCapable(class_loader)) {
1162       k = find_or_define_instance_class(class_name, class_loader, k, THREAD);
1163     } else {
1164       define_instance_class(k, THREAD);
1165     }
1166   }
1167 
1168   // Make sure we have an entry in the SystemDictionary on success
1169   debug_only( {
1170     if (!HAS_PENDING_EXCEPTION) {
1171       assert(parsed_name != NULL, "parsed_name is still null?");
1172       Symbol*  h_name    = k->name();
1173       ClassLoaderData *defining_loader_data = k->class_loader_data();
1174 
1175       MutexLocker mu(SystemDictionary_lock, THREAD);
1176 
1177       Klass* check = find_class(parsed_name, loader_data);


1320     // Search the shared system dictionary for classes preloaded into the
1321     // shared spaces.
1322     instanceKlassHandle k;
1323     {
1324 #if INCLUDE_CDS
1325       PerfTraceTime vmtimer(ClassLoader::perf_shared_classload_time());
1326       k = load_shared_class(class_name, class_loader, THREAD);
1327 #endif
1328     }
1329 
1330     if (k.is_null()) {
1331       // Use VM class loader
1332       PerfTraceTime vmtimer(ClassLoader::perf_sys_classload_time());
1333       k = ClassLoader::load_classfile(class_name, CHECK_(nh));
1334     }
1335 
1336     // find_or_define_instance_class may return a different InstanceKlass
1337     if (!k.is_null()) {
1338       k = find_or_define_instance_class(class_name, class_loader, k, CHECK_(nh));
1339     }
1340 
1341 #if INCLUDE_JFR
1342     if (k.is_null() && (class_name == jfr_event_handler_proxy)) {
1343       assert(jfr_event_handler_proxy != NULL, "invariant");
1344       // EventHandlerProxy class is generated dynamically in
1345       // EventHandlerProxyCreator::makeEventHandlerProxyClass
1346       // method, so we generate a Java call from here.
1347       //
1348       // EventHandlerProxy class will finally be defined in
1349       // SystemDictionary::resolve_from_stream method, down
1350       // the call stack. Bootstrap classloader is parallel-capable,
1351       // so no concurrency issues are expected.
1352       CLEAR_PENDING_EXCEPTION;
1353       k = JfrUpcalls::load_event_handler_proxy_class(THREAD);
1354       assert(!k.is_null(), "invariant");
1355     }
1356 #endif
1357 
1358     return k;
1359   } else {
1360     // Use user specified class loader to load class. Call loadClass operation on class_loader.
1361     ResourceMark rm(THREAD);
1362 
1363     assert(THREAD->is_Java_thread(), "must be a JavaThread");
1364     JavaThread* jt = (JavaThread*) THREAD;
1365 
1366     PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
1367                                ClassLoader::perf_app_classload_selftime(),
1368                                ClassLoader::perf_app_classload_count(),
1369                                jt->get_thread_stat()->perf_recursion_counts_addr(),
1370                                jt->get_thread_stat()->perf_timers_addr(),
1371                                PerfClassTraceTime::CLASS_LOAD);
1372 
1373     Handle s = java_lang_String::create_from_symbol(class_name, CHECK_(nh));
1374     // Translate to external class name format, i.e., convert '/' chars to '.'
1375     Handle string = java_lang_String::externalize_classname(s, CHECK_(nh));
1376 
1377     JavaValue result(T_OBJECT);


1416     assert(result.get_type() == T_OBJECT, "just checking");
1417     oop obj = (oop) result.get_jobject();
1418 
1419     // Primitive classes return null since forName() can not be
1420     // used to obtain any of the Class objects representing primitives or void
1421     if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
1422       instanceKlassHandle k =
1423                 instanceKlassHandle(THREAD, java_lang_Class::as_Klass(obj));
1424       // For user defined Java class loaders, check that the name returned is
1425       // the same as that requested.  This check is done for the bootstrap
1426       // loader when parsing the class file.
1427       if (class_name == k->name()) {
1428         return k;
1429       }
1430     }
1431     // Class is not found or has the wrong name, return NULL
1432     return nh;
1433   }
1434 }
1435 
1436 static void post_class_define_event(InstanceKlass* k, const ClassLoaderData* def_cld) {
1437   EventClassDefine event;
1438   if (event.should_commit()) {
1439     event.set_definedClass(k);
1440     event.set_definingClassLoader(def_cld);
1441     event.commit();
1442   }
1443 }
1444 
1445 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) {
1446 
1447   ClassLoaderData* loader_data = k->class_loader_data();
1448   Handle class_loader_h(THREAD, loader_data->class_loader());
1449 
1450   for (uintx it = 0; it < GCExpandToAllocateDelayMillis; it++){}
1451 
1452  // for bootstrap and other parallel classloaders don't acquire lock,
1453  // use placeholder token
1454  // If a parallelCapable class loader calls define_instance_class instead of
1455  // find_or_define_instance_class to get here, we have a timing
1456  // hole with systemDictionary updates and check_constraints
1457  if (!class_loader_h.is_null() && !is_parallelCapable(class_loader_h)) {
1458     assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD,
1459          compute_loader_lock_object(class_loader_h, THREAD)),
1460          "define called without lock");
1461   }
1462 
1463   // Check class-loading constraints. Throw exception if violation is detected.
1464   // Grabs and releases SystemDictionary_lock


1495     MutexLocker mu_r(Compile_lock, THREAD);
1496 
1497     // Add to class hierarchy, initialize vtables, and do possible
1498     // deoptimizations.
1499     add_to_hierarchy(k, CHECK); // No exception, but can block
1500 
1501     // Add to systemDictionary - so other classes can see it.
1502     // Grabs and releases SystemDictionary_lock
1503     update_dictionary(d_index, d_hash, p_index, p_hash,
1504                       k, class_loader_h, THREAD);
1505   }
1506   k->eager_initialize(THREAD);
1507 
1508   // notify jvmti
1509   if (JvmtiExport::should_post_class_load()) {
1510       assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1511       JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1512 
1513   }
1514 
1515   post_class_define_event(k(), loader_data);
1516 }
1517 
1518 // Support parallel classloading
1519 // All parallel class loaders, including bootstrap classloader
1520 // lock a placeholder entry for this class/class_loader pair
1521 // to allow parallel defines of different classes for this class loader
1522 // With AllowParallelDefine flag==true, in case they do not synchronize around
1523 // FindLoadedClass/DefineClass, calls, we check for parallel
1524 // loading for them, wait if a defineClass is in progress
1525 // and return the initial requestor's results
1526 // This flag does not apply to the bootstrap classloader.
1527 // With AllowParallelDefine flag==false, call through to define_instance_class
1528 // which will throw LinkageError: duplicate class definition.
1529 // False is the requested default.
1530 // For better performance, the class loaders should synchronize
1531 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they
1532 // potentially waste time reading and parsing the bytestream.
1533 // Note: VM callers should ensure consistency of k/class_name,class_loader
1534 instanceKlassHandle SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader, instanceKlassHandle k, TRAPS) {
1535 


1757 
1758   template <class T> void do_oop_work(T* p) {
1759     oop obj = oopDesc::load_decode_heap_oop(p);
1760     guarantee(_is_alive->do_object_b(obj), "Oop in system dictionary must be live");
1761   }
1762 
1763 public:
1764   VerifySDReachableAndLiveClosure(BoolObjectClosure* is_alive) : OopClosure(), _is_alive(is_alive) { }
1765 
1766   virtual void do_oop(oop* p)       { do_oop_work(p); }
1767   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
1768 };
1769 #endif
1770 
1771 // Assumes classes in the SystemDictionary are only unloaded at a safepoint
1772 // Note: anonymous classes are not in the SD.
1773 bool SystemDictionary::do_unloading(BoolObjectClosure* is_alive, bool clean_alive) {
1774   // First, mark for unload all ClassLoaderData referencing a dead class loader.
1775   bool unloading_occurred = ClassLoaderDataGraph::do_unloading(is_alive, clean_alive);
1776   if (unloading_occurred) {
1777     JFR_ONLY(Jfr::on_unloading_classes();)
1778     dictionary()->do_unloading();
1779     constraints()->purge_loader_constraints();
1780     resolution_errors()->purge_resolution_errors();
1781   }
1782   // Oops referenced by the system dictionary may get unreachable independently
1783   // of the class loader (eg. cached protection domain oops). So we need to
1784   // explicitly unlink them here instead of in Dictionary::do_unloading.
1785   dictionary()->unlink(is_alive);
1786 #ifdef ASSERT
1787   VerifySDReachableAndLiveClosure cl(is_alive);
1788   dictionary()->oops_do(&cl);
1789 #endif
1790   return unloading_occurred;
1791 }
1792 
1793 void SystemDictionary::roots_oops_do(OopClosure* strong, OopClosure* weak) {
1794   strong->do_oop(&_java_system_loader);
1795   strong->do_oop(&_system_loader_lock_obj);
1796   CDS_ONLY(SystemDictionaryShared::roots_oops_do(strong);)
1797 


1891 
1892 // ----------------------------------------------------------------------------
1893 // Initialization
1894 
1895 void SystemDictionary::initialize(TRAPS) {
1896   // Allocate arrays
1897   assert(dictionary() == NULL,
1898          "SystemDictionary should only be initialized once");
1899   _sdgeneration        = 0;
1900   _dictionary          = new Dictionary(calculate_systemdictionary_size(PredictedLoadedClassCount));
1901   _placeholders        = new PlaceholderTable(_nof_buckets);
1902   _number_of_modifications = 0;
1903   _loader_constraints  = new LoaderConstraintTable(_loader_constraint_size);
1904   _resolution_errors   = new ResolutionErrorTable(_resolution_error_size);
1905   _invoke_method_table = new SymbolPropertyTable(_invoke_method_size);
1906 
1907   // Allocate private object used as system class loader lock
1908   _system_loader_lock_obj = oopFactory::new_intArray(0, CHECK);
1909   // Initialize basic classes
1910   initialize_preloaded_classes(CHECK);
1911 #if INCLUDE_JFR
1912   jfr_event_handler_proxy = SymbolTable::new_permanent_symbol("jdk/jfr/proxy/internal/EventHandlerProxy", CHECK);
1913 #endif // INCLUDE_JFR
1914 }
1915 
1916 // Compact table of directions on the initialization of klasses:
1917 static const short wk_init_info[] = {
1918   #define WK_KLASS_INIT_INFO(name, symbol, option) \
1919     ( ((int)vmSymbols::VM_SYMBOL_ENUM_NAME(symbol) \
1920           << SystemDictionary::CEIL_LG_OPTION_LIMIT) \
1921       | (int)SystemDictionary::option ),
1922   WK_KLASSES_DO(WK_KLASS_INIT_INFO)
1923   #undef WK_KLASS_INIT_INFO
1924   0
1925 };
1926 
1927 bool SystemDictionary::initialize_wk_klass(WKID id, int init_opt, TRAPS) {
1928   assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
1929   int  info = wk_init_info[id - FIRST_WKID];
1930   int  sid  = (info >> CEIL_LG_OPTION_LIMIT);
1931   Symbol* symbol = vmSymbols::symbol_at((vmSymbols::SID)sid);
1932   Klass**    klassp = &_well_known_klasses[id];
1933   bool must_load = (init_opt < SystemDictionary::Opt);


2730 }
2731 
2732 
2733 void SystemDictionary::verify() {
2734   guarantee(dictionary() != NULL, "Verify of system dictionary failed");
2735   guarantee(constraints() != NULL,
2736             "Verify of loader constraints failed");
2737   guarantee(dictionary()->number_of_entries() >= 0 &&
2738             placeholders()->number_of_entries() >= 0,
2739             "Verify of system dictionary failed");
2740 
2741   // Verify dictionary
2742   dictionary()->verify();
2743 
2744   GCMutexLocker mu(SystemDictionary_lock);
2745   placeholders()->verify();
2746 
2747   // Verify constraint table
2748   guarantee(constraints() != NULL, "Verify of loader constraints failed");
2749   constraints()->verify(dictionary(), placeholders());




















2750 }
2751 
2752 #ifndef PRODUCT
2753 
2754 // statistics code
2755 class ClassStatistics: AllStatic {
2756  private:
2757   static int nclasses;        // number of classes
2758   static int nmethods;        // number of methods
2759   static int nmethoddata;     // number of methodData
2760   static int class_size;      // size of class objects in words
2761   static int method_size;     // size of method objects in words
2762   static int debug_size;      // size of debug info in methods
2763   static int methoddata_size; // size of methodData objects in words
2764 
2765   static void do_class(Klass* k) {
2766     nclasses++;
2767     class_size += k->size();
2768     if (k->oop_is_instance()) {
2769       InstanceKlass* ik = (InstanceKlass*)k;


< prev index next >