hotspot/src/share/vm/classfile/systemDictionary.cpp
changeset 46729 c62d2e8b2728
parent 46701 f559541c0daa
child 46742 24ec8a039c90
equal deleted inserted replaced
46728:a1bee305515d 46729:c62d2e8b2728
    33 #include "classfile/javaClasses.inline.hpp"
    33 #include "classfile/javaClasses.inline.hpp"
    34 #include "classfile/klassFactory.hpp"
    34 #include "classfile/klassFactory.hpp"
    35 #include "classfile/loaderConstraints.hpp"
    35 #include "classfile/loaderConstraints.hpp"
    36 #include "classfile/packageEntry.hpp"
    36 #include "classfile/packageEntry.hpp"
    37 #include "classfile/placeholders.hpp"
    37 #include "classfile/placeholders.hpp"
       
    38 #include "classfile/protectionDomainCache.hpp"
    38 #include "classfile/resolutionErrors.hpp"
    39 #include "classfile/resolutionErrors.hpp"
    39 #include "classfile/stringTable.hpp"
    40 #include "classfile/stringTable.hpp"
    40 #include "classfile/systemDictionary.hpp"
    41 #include "classfile/systemDictionary.hpp"
    41 #include "classfile/vmSymbols.hpp"
    42 #include "classfile/vmSymbols.hpp"
    42 #include "code/codeCache.hpp"
    43 #include "code/codeCache.hpp"
    86 #endif
    87 #endif
    87 #if INCLUDE_TRACE
    88 #if INCLUDE_TRACE
    88 #include "trace/tracing.hpp"
    89 #include "trace/tracing.hpp"
    89 #endif
    90 #endif
    90 
    91 
    91 Dictionary*            SystemDictionary::_dictionary          = NULL;
       
    92 PlaceholderTable*      SystemDictionary::_placeholders        = NULL;
    92 PlaceholderTable*      SystemDictionary::_placeholders        = NULL;
    93 Dictionary*            SystemDictionary::_shared_dictionary   = NULL;
    93 Dictionary*            SystemDictionary::_shared_dictionary   = NULL;
    94 LoaderConstraintTable* SystemDictionary::_loader_constraints  = NULL;
    94 LoaderConstraintTable* SystemDictionary::_loader_constraints  = NULL;
    95 ResolutionErrorTable*  SystemDictionary::_resolution_errors   = NULL;
    95 ResolutionErrorTable*  SystemDictionary::_resolution_errors   = NULL;
    96 SymbolPropertyTable*   SystemDictionary::_invoke_method_table = NULL;
    96 SymbolPropertyTable*   SystemDictionary::_invoke_method_table = NULL;
    97 
    97 ProtectionDomainCacheTable*   SystemDictionary::_pd_cache_table = NULL;
    98 
    98 
    99 int         SystemDictionary::_number_of_modifications = 0;
    99 int         SystemDictionary::_number_of_modifications = 0;
   100 int         SystemDictionary::_sdgeneration               = 0;
       
   101 const int   SystemDictionary::_primelist[_prime_array_size] = {1009,2017,4049,5051,10103,
       
   102               20201,40423,99991};
       
   103 
       
   104 oop         SystemDictionary::_system_loader_lock_obj     =  NULL;
   100 oop         SystemDictionary::_system_loader_lock_obj     =  NULL;
   105 
   101 
   106 InstanceKlass*      SystemDictionary::_well_known_klasses[SystemDictionary::WKID_LIMIT]
   102 InstanceKlass*      SystemDictionary::_well_known_klasses[SystemDictionary::WKID_LIMIT]
   107                                                           =  { NULL /*, NULL...*/ };
   103                                                           =  { NULL /*, NULL...*/ };
   108 
   104 
   113 bool        SystemDictionary::_has_loadClassInternal      =  false;
   109 bool        SystemDictionary::_has_loadClassInternal      =  false;
   114 bool        SystemDictionary::_has_checkPackageAccess     =  false;
   110 bool        SystemDictionary::_has_checkPackageAccess     =  false;
   115 
   111 
   116 // lazily initialized klass variables
   112 // lazily initialized klass variables
   117 InstanceKlass* volatile SystemDictionary::_abstract_ownable_synchronizer_klass = NULL;
   113 InstanceKlass* volatile SystemDictionary::_abstract_ownable_synchronizer_klass = NULL;
       
   114 
       
   115 // Default ProtectionDomainCacheSize value
       
   116 
       
   117 const int defaultProtectionDomainCacheSize = 1009;
   118 
   118 
   119 
   119 
   120 // ----------------------------------------------------------------------------
   120 // ----------------------------------------------------------------------------
   121 // Java-level SystemLoader
   121 // Java-level SystemLoader
   122 
   122 
   341     }
   341     }
   342   }
   342   }
   343 #endif // INCLUDE_CDS
   343 #endif // INCLUDE_CDS
   344 
   344 
   345   // Double-check, if child class is already loaded, just return super-class,interface
   345   // Double-check, if child class is already loaded, just return super-class,interface
   346   // Don't add a placedholder if already loaded, i.e. already in system dictionary
   346   // Don't add a placedholder if already loaded, i.e. already in appropriate class loader
       
   347   // dictionary.
   347   // Make sure there's a placeholder for the *child* before resolving.
   348   // Make sure there's a placeholder for the *child* before resolving.
   348   // Used as a claim that this thread is currently loading superclass/classloader
   349   // Used as a claim that this thread is currently loading superclass/classloader
   349   // Used here for ClassCircularity checks and also for heap verification
   350   // Used here for ClassCircularity checks and also for heap verification
   350   // (every InstanceKlass in the heap needs to be in the system dictionary
   351   // (every InstanceKlass needs to be in its class loader dictionary or have a placeholder).
   351   // or have a placeholder).
   352   // Must check ClassCircularity before checking if super class is already loaded.
   352   // Must check ClassCircularity before checking if super class is already loaded
       
   353   //
   353   //
   354   // We might not already have a placeholder if this child_name was
   354   // We might not already have a placeholder if this child_name was
   355   // first seen via resolve_from_stream (jni_DefineClass or JVM_DefineClass);
   355   // first seen via resolve_from_stream (jni_DefineClass or JVM_DefineClass);
   356   // the name of the class might not be known until the stream is actually
   356   // the name of the class might not be known until the stream is actually
   357   // parsed.
   357   // parsed.
   358   // Bugs 4643874, 4715493
   358   // Bugs 4643874, 4715493
   359   // compute_hash can have a safepoint
       
   360 
   359 
   361   ClassLoaderData* loader_data = class_loader_data(class_loader);
   360   ClassLoaderData* loader_data = class_loader_data(class_loader);
   362   unsigned int d_hash = dictionary()->compute_hash(child_name, loader_data);
   361   Dictionary* dictionary = loader_data->dictionary();
   363   int d_index = dictionary()->hash_to_index(d_hash);
   362   unsigned int d_hash = dictionary->compute_hash(child_name);
   364   unsigned int p_hash = placeholders()->compute_hash(child_name, loader_data);
   363   int d_index = dictionary->hash_to_index(d_hash);
       
   364   unsigned int p_hash = placeholders()->compute_hash(child_name);
   365   int p_index = placeholders()->hash_to_index(p_hash);
   365   int p_index = placeholders()->hash_to_index(p_hash);
   366   // can't throw error holding a lock
   366   // can't throw error holding a lock
   367   bool child_already_loaded = false;
   367   bool child_already_loaded = false;
   368   bool throw_circularity_error = false;
   368   bool throw_circularity_error = false;
   369   {
   369   {
   370     MutexLocker mu(SystemDictionary_lock, THREAD);
   370     MutexLocker mu(SystemDictionary_lock, THREAD);
   371     Klass* childk = find_class(d_index, d_hash, child_name, loader_data);
   371     Klass* childk = find_class(d_index, d_hash, child_name, dictionary);
   372     Klass* quicksuperk;
   372     Klass* quicksuperk;
   373     // to support // loading: if child done loading, just return superclass
   373     // to support // loading: if child done loading, just return superclass
   374     // if class_name, & class_loader don't match:
   374     // if class_name, & class_loader don't match:
   375     // if initial define, SD update will give LinkageError
   375     // if initial define, SD update will give LinkageError
   376     // if redefine: compare_class_versions will give HIERARCHY_CHANGED
   376     // if redefine: compare_class_versions will give HIERARCHY_CHANGED
   443     ls.print(" protection domain: "); protection_domain()->print_value_on(&ls);
   443     ls.print(" protection domain: "); protection_domain()->print_value_on(&ls);
   444     ls.print(" loading: "); klass->print_value_on(&ls);
   444     ls.print(" loading: "); klass->print_value_on(&ls);
   445     ls.cr();
   445     ls.cr();
   446   }
   446   }
   447 
   447 
       
   448   // This handle and the class_loader handle passed in keeps this class from
       
   449   // being unloaded through several GC points.
       
   450   // The class_loader handle passed in is the initiating loader.
       
   451   Handle mirror(THREAD, klass->java_mirror());
       
   452 
   448   InstanceKlass* system_loader = SystemDictionary::ClassLoader_klass();
   453   InstanceKlass* system_loader = SystemDictionary::ClassLoader_klass();
   449   JavaCalls::call_special(&result,
   454   JavaCalls::call_special(&result,
   450                          class_loader,
   455                          class_loader,
   451                          system_loader,
   456                          system_loader,
   452                          vmSymbols::checkPackageAccess_name(),
   457                          vmSymbols::checkPackageAccess_name(),
   453                          vmSymbols::class_protectiondomain_signature(),
   458                          vmSymbols::class_protectiondomain_signature(),
   454                          Handle(THREAD, klass->java_mirror()),
   459                          mirror,
   455                          protection_domain,
   460                          protection_domain,
   456                          THREAD);
   461                          THREAD);
   457 
   462 
   458   if (HAS_PENDING_EXCEPTION) {
   463   if (HAS_PENDING_EXCEPTION) {
   459     log_debug(protectiondomain)("DENIED !!!!!!!!!!!!!!!!!!!!!");
   464     log_debug(protectiondomain)("DENIED !!!!!!!!!!!!!!!!!!!!!");
   464   if (HAS_PENDING_EXCEPTION) return;
   469   if (HAS_PENDING_EXCEPTION) return;
   465 
   470 
   466   // If no exception has been thrown, we have validated the protection domain
   471   // If no exception has been thrown, we have validated the protection domain
   467   // Insert the protection domain of the initiating class into the set.
   472   // Insert the protection domain of the initiating class into the set.
   468   {
   473   {
   469     // We recalculate the entry here -- we've called out to java since
       
   470     // the last time it was calculated.
       
   471     ClassLoaderData* loader_data = class_loader_data(class_loader);
   474     ClassLoaderData* loader_data = class_loader_data(class_loader);
       
   475     Dictionary* dictionary = loader_data->dictionary();
   472 
   476 
   473     Symbol*  kn = klass->name();
   477     Symbol*  kn = klass->name();
   474     unsigned int d_hash = dictionary()->compute_hash(kn, loader_data);
   478     unsigned int d_hash = dictionary->compute_hash(kn);
   475     int d_index = dictionary()->hash_to_index(d_hash);
   479     int d_index = dictionary->hash_to_index(d_hash);
   476 
   480 
   477     MutexLocker mu(SystemDictionary_lock, THREAD);
   481     MutexLocker mu(SystemDictionary_lock, THREAD);
   478     {
   482     dictionary->add_protection_domain(d_index, d_hash, klass,
   479       // Note that we have an entry, and entries can be deleted only during GC,
   483                                       protection_domain, THREAD);
   480       // so we cannot allow GC to occur while we're holding this entry.
       
   481 
       
   482       // We're using a NoSafepointVerifier to catch any place where we
       
   483       // might potentially do a GC at all.
       
   484       // Dictionary::do_unloading() asserts that classes in SD are only
       
   485       // unloaded at a safepoint. Anonymous classes are not in SD.
       
   486       NoSafepointVerifier nosafepoint;
       
   487       dictionary()->add_protection_domain(d_index, d_hash, klass, loader_data,
       
   488                                           protection_domain, THREAD);
       
   489     }
       
   490   }
   484   }
   491 }
   485 }
   492 
   486 
   493 // We only get here if this thread finds that another thread
   487 // We only get here if this thread finds that another thread
   494 // has already claimed the placeholder token for the current operation,
   488 // has already claimed the placeholder token for the current operation,
   546 InstanceKlass* SystemDictionary::handle_parallel_super_load(
   540 InstanceKlass* SystemDictionary::handle_parallel_super_load(
   547     Symbol* name, Symbol* superclassname, Handle class_loader,
   541     Symbol* name, Symbol* superclassname, Handle class_loader,
   548     Handle protection_domain, Handle lockObject, TRAPS) {
   542     Handle protection_domain, Handle lockObject, TRAPS) {
   549 
   543 
   550   ClassLoaderData* loader_data = class_loader_data(class_loader);
   544   ClassLoaderData* loader_data = class_loader_data(class_loader);
   551   unsigned int d_hash = dictionary()->compute_hash(name, loader_data);
   545   Dictionary* dictionary = loader_data->dictionary();
   552   int d_index = dictionary()->hash_to_index(d_hash);
   546   unsigned int d_hash = dictionary->compute_hash(name);
   553   unsigned int p_hash = placeholders()->compute_hash(name, loader_data);
   547   int d_index = dictionary->hash_to_index(d_hash);
       
   548   unsigned int p_hash = placeholders()->compute_hash(name);
   554   int p_index = placeholders()->hash_to_index(p_hash);
   549   int p_index = placeholders()->hash_to_index(p_hash);
   555 
   550 
   556   // superk is not used, resolve_super called for circularity check only
   551   // superk is not used, resolve_super called for circularity check only
   557   // This code is reached in two situations. One if this thread
   552   // This code is reached in two situations. One if this thread
   558   // is loading the same class twice (e.g. ClassCircularity, or
   553   // is loading the same class twice (e.g. ClassCircularity, or
   571   // parallelCapable class loaders do NOT wait for parallel superclass loads to complete
   566   // parallelCapable class loaders do NOT wait for parallel superclass loads to complete
   572   // Serial class loaders and bootstrap classloader do wait for superclass loads
   567   // Serial class loaders and bootstrap classloader do wait for superclass loads
   573  if (!class_loader.is_null() && is_parallelCapable(class_loader)) {
   568  if (!class_loader.is_null() && is_parallelCapable(class_loader)) {
   574     MutexLocker mu(SystemDictionary_lock, THREAD);
   569     MutexLocker mu(SystemDictionary_lock, THREAD);
   575     // Check if classloading completed while we were loading superclass or waiting
   570     // Check if classloading completed while we were loading superclass or waiting
   576     return find_class(d_index, d_hash, name, loader_data);
   571     return find_class(d_index, d_hash, name, dictionary);
   577   }
   572   }
   578 
   573 
   579   // must loop to both handle other placeholder updates
   574   // must loop to both handle other placeholder updates
   580   // and spurious notifications
   575   // and spurious notifications
   581   bool super_load_in_progress = true;
   576   bool super_load_in_progress = true;
   582   PlaceholderEntry* placeholder;
   577   PlaceholderEntry* placeholder;
   583   while (super_load_in_progress) {
   578   while (super_load_in_progress) {
   584     MutexLocker mu(SystemDictionary_lock, THREAD);
   579     MutexLocker mu(SystemDictionary_lock, THREAD);
   585     // Check if classloading completed while we were loading superclass or waiting
   580     // Check if classloading completed while we were loading superclass or waiting
   586     InstanceKlass* check = find_class(d_index, d_hash, name, loader_data);
   581     InstanceKlass* check = find_class(d_index, d_hash, name, dictionary);
   587     if (check != NULL) {
   582     if (check != NULL) {
   588       // Klass is already loaded, so just return it
   583       // Klass is already loaded, so just return it
   589       return check;
   584       return check;
   590     } else {
   585     } else {
   591       placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data);
   586       placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data);
   661   HandleMark hm(THREAD);
   656   HandleMark hm(THREAD);
   662 
   657 
   663   // Fix for 4474172; see evaluation for more details
   658   // Fix for 4474172; see evaluation for more details
   664   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
   659   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
   665   ClassLoaderData *loader_data = register_loader(class_loader, CHECK_NULL);
   660   ClassLoaderData *loader_data = register_loader(class_loader, CHECK_NULL);
       
   661   Dictionary* dictionary = loader_data->dictionary();
   666 
   662 
   667   // Do lookup to see if class already exist and the protection domain
   663   // Do lookup to see if class already exist and the protection domain
   668   // has the right access
   664   // has the right access
   669   // This call uses find which checks protection domain already matches
   665   // This call uses find which checks protection domain already matches
   670   // All subsequent calls use find_class, and set has_loaded_class so that
   666   // All subsequent calls use find_class, and set has_loaded_class so that
   671   // before we return a result we call out to java to check for valid protection domain
   667   // before we return a result we call out to java to check for valid protection domain
   672   // to allow returning the Klass* and add it to the pd_set if it is valid
   668   // to allow returning the Klass* and add it to the pd_set if it is valid
   673   unsigned int d_hash = dictionary()->compute_hash(name, loader_data);
   669   unsigned int d_hash = dictionary->compute_hash(name);
   674   int d_index = dictionary()->hash_to_index(d_hash);
   670   int d_index = dictionary->hash_to_index(d_hash);
   675   Klass* probe = dictionary()->find(d_index, d_hash, name, loader_data,
   671   Klass* probe = dictionary->find(d_index, d_hash, name, protection_domain);
   676                                       protection_domain, THREAD);
       
   677   if (probe != NULL) return probe;
   672   if (probe != NULL) return probe;
   678 
   673 
   679 
   674 
   680   // Non-bootstrap class loaders will call out to class loader and
   675   // Non-bootstrap class loaders will call out to class loader and
   681   // define via jvm/jni_DefineClass which will acquire the
   676   // define via jvm/jni_DefineClass which will acquire the
   689   bool DoObjectLock = true;
   684   bool DoObjectLock = true;
   690   if (is_parallelCapable(class_loader)) {
   685   if (is_parallelCapable(class_loader)) {
   691     DoObjectLock = false;
   686     DoObjectLock = false;
   692   }
   687   }
   693 
   688 
   694   unsigned int p_hash = placeholders()->compute_hash(name, loader_data);
   689   unsigned int p_hash = placeholders()->compute_hash(name);
   695   int p_index = placeholders()->hash_to_index(p_hash);
   690   int p_index = placeholders()->hash_to_index(p_hash);
   696 
   691 
   697   // Class is not in SystemDictionary so we have to do loading.
   692   // Class is not in SystemDictionary so we have to do loading.
   698   // Make sure we are synchronized on the class loader before we proceed
   693   // Make sure we are synchronized on the class loader before we proceed
   699   Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
   694   Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
   708   PlaceholderEntry* placeholder;
   703   PlaceholderEntry* placeholder;
   709   Symbol* superclassname = NULL;
   704   Symbol* superclassname = NULL;
   710 
   705 
   711   {
   706   {
   712     MutexLocker mu(SystemDictionary_lock, THREAD);
   707     MutexLocker mu(SystemDictionary_lock, THREAD);
   713     InstanceKlass* check = find_class(d_index, d_hash, name, loader_data);
   708     InstanceKlass* check = find_class(d_index, d_hash, name, dictionary);
   714     if (check != NULL) {
   709     if (check != NULL) {
   715       // Klass is already loaded, so just return it
   710       // Klass is already loaded, so just return it
   716       class_has_been_loaded = true;
   711       class_has_been_loaded = true;
   717       k = check;
   712       k = check;
   718     } else {
   713     } else {
   792               // case 2: traditional with broken classloader lock. wait on first
   787               // case 2: traditional with broken classloader lock. wait on first
   793               // requestor.
   788               // requestor.
   794                 double_lock_wait(lockObject, THREAD);
   789                 double_lock_wait(lockObject, THREAD);
   795               }
   790               }
   796               // Check if classloading completed while we were waiting
   791               // Check if classloading completed while we were waiting
   797               InstanceKlass* check = find_class(d_index, d_hash, name, loader_data);
   792               InstanceKlass* check = find_class(d_index, d_hash, name, dictionary);
   798               if (check != NULL) {
   793               if (check != NULL) {
   799                 // Klass is already loaded, so just return it
   794                 // Klass is already loaded, so just return it
   800                 k = check;
   795                 k = check;
   801                 class_has_been_loaded = true;
   796                 class_has_been_loaded = true;
   802               }
   797               }
   817         // if they did not catch another thread holding LOAD_INSTANCE,
   812         // if they did not catch another thread holding LOAD_INSTANCE,
   818         // need a check analogous to the acquire ObjectLocker/find_class
   813         // need a check analogous to the acquire ObjectLocker/find_class
   819         // i.e. now that we hold the LOAD_INSTANCE token on loading this class/CL
   814         // i.e. now that we hold the LOAD_INSTANCE token on loading this class/CL
   820         // one final check if the load has already completed
   815         // one final check if the load has already completed
   821         // class loaders holding the ObjectLock shouldn't find the class here
   816         // class loaders holding the ObjectLock shouldn't find the class here
   822         InstanceKlass* check = find_class(d_index, d_hash, name, loader_data);
   817         InstanceKlass* check = find_class(d_index, d_hash, name, dictionary);
   823         if (check != NULL) {
   818         if (check != NULL) {
   824         // Klass is already loaded, so return it after checking/adding protection domain
   819         // Klass is already loaded, so return it after checking/adding protection domain
   825           k = check;
   820           k = check;
   826           class_has_been_loaded = true;
   821           class_has_been_loaded = true;
   827         }
   822         }
   850       // Bootstrap goes through here to allow for an extra guarantee check
   845       // Bootstrap goes through here to allow for an extra guarantee check
   851       if (UnsyncloadClass || (class_loader.is_null())) {
   846       if (UnsyncloadClass || (class_loader.is_null())) {
   852         if (k == NULL && HAS_PENDING_EXCEPTION
   847         if (k == NULL && HAS_PENDING_EXCEPTION
   853           && PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
   848           && PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
   854           MutexLocker mu(SystemDictionary_lock, THREAD);
   849           MutexLocker mu(SystemDictionary_lock, THREAD);
   855           InstanceKlass* check = find_class(d_index, d_hash, name, loader_data);
   850           InstanceKlass* check = find_class(d_index, d_hash, name, dictionary);
   856           if (check != NULL) {
   851           if (check != NULL) {
   857             // Klass is already loaded, so just use it
   852             // Klass is already loaded, so just use it
   858             k = check;
   853             k = check;
   859             CLEAR_PENDING_EXCEPTION;
   854             CLEAR_PENDING_EXCEPTION;
   860             guarantee((!class_loader.is_null()), "dup definition for bootstrap loader?");
   855             guarantee((!class_loader.is_null()), "dup definition for bootstrap loader?");
   884             assert(thread->is_Java_thread(), "thread->is_Java_thread()");
   879             assert(thread->is_Java_thread(), "thread->is_Java_thread()");
   885             JvmtiExport::post_class_load((JavaThread *) thread, k);
   880             JvmtiExport::post_class_load((JavaThread *) thread, k);
   886           }
   881           }
   887         }
   882         }
   888       }
   883       }
   889     } // load_instance_class loop
   884     } // load_instance_class
   890 
   885 
   891     if (load_instance_added == true) {
   886     if (load_instance_added == true) {
   892       // clean up placeholder entries for LOAD_INSTANCE success or error
   887       // clean up placeholder entries for LOAD_INSTANCE success or error
   893       // This brackets the SystemDictionary updates for both defining
   888       // This brackets the SystemDictionary updates for both defining
   894       // and initiating loaders
   889       // and initiating loaders
   917   if (protection_domain() == NULL) return k;
   912   if (protection_domain() == NULL) return k;
   918 
   913 
   919   // Check the protection domain has the right access
   914   // Check the protection domain has the right access
   920   {
   915   {
   921     MutexLocker mu(SystemDictionary_lock, THREAD);
   916     MutexLocker mu(SystemDictionary_lock, THREAD);
   922     // Note that we have an entry, and entries can be deleted only during GC,
   917     if (dictionary->is_valid_protection_domain(d_index, d_hash, name,
   923     // so we cannot allow GC to occur while we're holding this entry.
   918                                                protection_domain)) {
   924     // We're using a NoSafepointVerifier to catch any place where we
       
   925     // might potentially do a GC at all.
       
   926     // Dictionary::do_unloading() asserts that classes in SD are only
       
   927     // unloaded at a safepoint. Anonymous classes are not in SD.
       
   928     NoSafepointVerifier nosafepoint;
       
   929     if (dictionary()->is_valid_protection_domain(d_index, d_hash, name,
       
   930                                                  loader_data,
       
   931                                                  protection_domain)) {
       
   932       return k;
   919       return k;
   933     }
   920     }
   934   }
   921   }
   935 
   922 
   936   // Verify protection domain. If it fails an exception is thrown
   923   // Verify protection domain. If it fails an exception is thrown
   966     // If the ClassLoaderData has not been setup,
   953     // If the ClassLoaderData has not been setup,
   967     // then the class loader has no entries in the dictionary.
   954     // then the class loader has no entries in the dictionary.
   968     return NULL;
   955     return NULL;
   969   }
   956   }
   970 
   957 
   971   unsigned int d_hash = dictionary()->compute_hash(class_name, loader_data);
   958   Dictionary* dictionary = loader_data->dictionary();
   972   int d_index = dictionary()->hash_to_index(d_hash);
   959   unsigned int d_hash = dictionary->compute_hash(class_name);
   973 
   960   int d_index = dictionary->hash_to_index(d_hash);
   974   {
   961   return dictionary->find(d_index, d_hash, class_name,
   975     // Note that we have an entry, and entries can be deleted only during GC,
   962                           protection_domain);
   976     // so we cannot allow GC to occur while we're holding this entry.
       
   977     // We're using a NoSafepointVerifier to catch any place where we
       
   978     // might potentially do a GC at all.
       
   979     // Dictionary::do_unloading() asserts that classes in SD are only
       
   980     // unloaded at a safepoint. Anonymous classes are not in SD.
       
   981     NoSafepointVerifier nosafepoint;
       
   982     return dictionary()->find(d_index, d_hash, class_name, loader_data,
       
   983                               protection_domain, THREAD);
       
   984   }
       
   985 }
   963 }
   986 
   964 
   987 
   965 
   988 // Look for a loaded instance or array klass by name.  Do not do any loading.
   966 // Look for a loaded instance or array klass by name.  Do not do any loading.
   989 // return NULL in case of error.
   967 // return NULL in case of error.
  1033     // Create a new CLD for anonymous class, that uses the same class loader
  1011     // Create a new CLD for anonymous class, that uses the same class loader
  1034     // as the host_klass
  1012     // as the host_klass
  1035     guarantee(host_klass->class_loader() == class_loader(), "should be the same");
  1013     guarantee(host_klass->class_loader() == class_loader(), "should be the same");
  1036     guarantee(!DumpSharedSpaces, "must not create anonymous classes when dumping");
  1014     guarantee(!DumpSharedSpaces, "must not create anonymous classes when dumping");
  1037     loader_data = ClassLoaderData::anonymous_class_loader_data(class_loader(), CHECK_NULL);
  1015     loader_data = ClassLoaderData::anonymous_class_loader_data(class_loader(), CHECK_NULL);
  1038     loader_data->record_dependency(host_klass, CHECK_NULL);
       
  1039   } else {
  1016   } else {
  1040     loader_data = ClassLoaderData::class_loader_data(class_loader());
  1017     loader_data = ClassLoaderData::class_loader_data(class_loader());
  1041   }
  1018   }
  1042 
  1019 
  1043   assert(st != NULL, "invariant");
  1020   assert(st != NULL, "invariant");
  1064 
  1041 
  1065       // Add to class hierarchy, initialize vtables, and do possible
  1042       // Add to class hierarchy, initialize vtables, and do possible
  1066       // deoptimizations.
  1043       // deoptimizations.
  1067       add_to_hierarchy(k, CHECK_NULL); // No exception, but can block
  1044       add_to_hierarchy(k, CHECK_NULL); // No exception, but can block
  1068 
  1045 
  1069       // But, do not add to system dictionary.
  1046       // But, do not add to dictionary.
  1070 
  1047 
  1071       // compiled code dependencies need to be validated anyway
  1048       // compiled code dependencies need to be validated anyway
  1072       notice_modification();
  1049       notice_modification();
  1073     }
  1050     }
  1074 
  1051 
  1187 }
  1164 }
  1188 
  1165 
  1189 #if INCLUDE_CDS
  1166 #if INCLUDE_CDS
  1190 void SystemDictionary::set_shared_dictionary(HashtableBucket<mtClass>* t, int length,
  1167 void SystemDictionary::set_shared_dictionary(HashtableBucket<mtClass>* t, int length,
  1191                                              int number_of_entries) {
  1168                                              int number_of_entries) {
  1192   assert(length == _nof_buckets * sizeof(HashtableBucket<mtClass>),
  1169   assert(length == _shared_dictionary_size * sizeof(HashtableBucket<mtClass>),
  1193          "bad shared dictionary size.");
  1170          "bad shared dictionary size.");
  1194   _shared_dictionary = new Dictionary(_nof_buckets, t, number_of_entries);
  1171   _shared_dictionary = new Dictionary(ClassLoaderData::the_null_class_loader_data(),
       
  1172                                       _shared_dictionary_size, t, number_of_entries);
  1195 }
  1173 }
  1196 
  1174 
  1197 
  1175 
  1198 // If there is a shared dictionary, then find the entry for the
  1176 // If there is a shared dictionary, then find the entry for the
  1199 // given shared system class, if any.
  1177 // given shared system class, if any.
  1200 
  1178 
  1201 InstanceKlass* SystemDictionary::find_shared_class(Symbol* class_name) {
  1179 InstanceKlass* SystemDictionary::find_shared_class(Symbol* class_name) {
  1202   if (shared_dictionary() != NULL) {
  1180   if (shared_dictionary() != NULL) {
  1203     unsigned int d_hash = shared_dictionary()->compute_hash(class_name, NULL);
  1181     unsigned int d_hash = shared_dictionary()->compute_hash(class_name);
  1204     int d_index = shared_dictionary()->hash_to_index(d_hash);
  1182     int d_index = shared_dictionary()->hash_to_index(d_hash);
  1205 
  1183 
  1206     return shared_dictionary()->find_shared_class(d_index, d_hash, class_name);
  1184     return shared_dictionary()->find_shared_class(d_index, d_hash, class_name);
  1207   } else {
  1185   } else {
  1208     return NULL;
  1186     return NULL;
  1624   // Existing classloaders will call define_instance_class with the
  1602   // Existing classloaders will call define_instance_class with the
  1625   // classloader lock held
  1603   // classloader lock held
  1626   // Parallel classloaders will call find_or_define_instance_class
  1604   // Parallel classloaders will call find_or_define_instance_class
  1627   // which will require a token to perform the define class
  1605   // which will require a token to perform the define class
  1628   Symbol*  name_h = k->name();
  1606   Symbol*  name_h = k->name();
  1629   unsigned int d_hash = dictionary()->compute_hash(name_h, loader_data);
  1607   Dictionary* dictionary = loader_data->dictionary();
  1630   int d_index = dictionary()->hash_to_index(d_hash);
  1608   unsigned int d_hash = dictionary->compute_hash(name_h);
       
  1609   int d_index = dictionary->hash_to_index(d_hash);
  1631   check_constraints(d_index, d_hash, k, class_loader_h, true, CHECK);
  1610   check_constraints(d_index, d_hash, k, class_loader_h, true, CHECK);
  1632 
  1611 
  1633   // Register class just loaded with class loader (placed in Vector)
  1612   // Register class just loaded with class loader (placed in Vector)
  1634   // Note we do this before updating the dictionary, as this can
  1613   // Note we do this before updating the dictionary, as this can
  1635   // fail with an OutOfMemoryError (if it does, we will *not* put this
  1614   // fail with an OutOfMemoryError (if it does, we will *not* put this
  1643     JavaCalls::call(&result, m, &args, CHECK);
  1622     JavaCalls::call(&result, m, &args, CHECK);
  1644   }
  1623   }
  1645 
  1624 
  1646   // Add the new class. We need recompile lock during update of CHA.
  1625   // Add the new class. We need recompile lock during update of CHA.
  1647   {
  1626   {
  1648     unsigned int p_hash = placeholders()->compute_hash(name_h, loader_data);
  1627     unsigned int p_hash = placeholders()->compute_hash(name_h);
  1649     int p_index = placeholders()->hash_to_index(p_hash);
  1628     int p_index = placeholders()->hash_to_index(p_hash);
  1650 
  1629 
  1651     MutexLocker mu_r(Compile_lock, THREAD);
  1630     MutexLocker mu_r(Compile_lock, THREAD);
  1652 
  1631 
  1653     // Add to class hierarchy, initialize vtables, and do possible
  1632     // Add to class hierarchy, initialize vtables, and do possible
  1693 InstanceKlass* SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader,
  1672 InstanceKlass* SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader,
  1694                                                                InstanceKlass* k, TRAPS) {
  1673                                                                InstanceKlass* k, TRAPS) {
  1695 
  1674 
  1696   Symbol*  name_h = k->name(); // passed in class_name may be null
  1675   Symbol*  name_h = k->name(); // passed in class_name may be null
  1697   ClassLoaderData* loader_data = class_loader_data(class_loader);
  1676   ClassLoaderData* loader_data = class_loader_data(class_loader);
  1698 
  1677   Dictionary* dictionary = loader_data->dictionary();
  1699   unsigned int d_hash = dictionary()->compute_hash(name_h, loader_data);
  1678 
  1700   int d_index = dictionary()->hash_to_index(d_hash);
  1679   unsigned int d_hash = dictionary->compute_hash(name_h);
       
  1680   int d_index = dictionary->hash_to_index(d_hash);
  1701 
  1681 
  1702   // Hold SD lock around find_class and placeholder creation for DEFINE_CLASS
  1682   // Hold SD lock around find_class and placeholder creation for DEFINE_CLASS
  1703   unsigned int p_hash = placeholders()->compute_hash(name_h, loader_data);
  1683   unsigned int p_hash = placeholders()->compute_hash(name_h);
  1704   int p_index = placeholders()->hash_to_index(p_hash);
  1684   int p_index = placeholders()->hash_to_index(p_hash);
  1705   PlaceholderEntry* probe;
  1685   PlaceholderEntry* probe;
  1706 
  1686 
  1707   {
  1687   {
  1708     MutexLocker mu(SystemDictionary_lock, THREAD);
  1688     MutexLocker mu(SystemDictionary_lock, THREAD);
  1709     // First check if class already defined
  1689     // First check if class already defined
  1710     if (UnsyncloadClass || (is_parallelDefine(class_loader))) {
  1690     if (UnsyncloadClass || (is_parallelDefine(class_loader))) {
  1711       InstanceKlass* check = find_class(d_index, d_hash, name_h, loader_data);
  1691       InstanceKlass* check = find_class(d_index, d_hash, name_h, dictionary);
  1712       if (check != NULL) {
  1692       if (check != NULL) {
  1713         return check;
  1693         return check;
  1714       }
  1694       }
  1715     }
  1695     }
  1716 
  1696 
  1728     // caught by finding an entry in the SystemDictionary
  1708     // caught by finding an entry in the SystemDictionary
  1729     if ((UnsyncloadClass || is_parallelDefine(class_loader)) && (probe->instance_klass() != NULL)) {
  1709     if ((UnsyncloadClass || is_parallelDefine(class_loader)) && (probe->instance_klass() != NULL)) {
  1730         placeholders()->find_and_remove(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, THREAD);
  1710         placeholders()->find_and_remove(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, THREAD);
  1731         SystemDictionary_lock->notify_all();
  1711         SystemDictionary_lock->notify_all();
  1732 #ifdef ASSERT
  1712 #ifdef ASSERT
  1733         InstanceKlass* check = find_class(d_index, d_hash, name_h, loader_data);
  1713         InstanceKlass* check = find_class(d_index, d_hash, name_h, dictionary);
  1734         assert(check != NULL, "definer missed recording success");
  1714         assert(check != NULL, "definer missed recording success");
  1735 #endif
  1715 #endif
  1736         return probe->instance_klass();
  1716         return probe->instance_klass();
  1737     } else {
  1717     } else {
  1738       // This thread will define the class (even if earlier thread tried and had an error)
  1718       // This thread will define the class (even if earlier thread tried and had an error)
  1767     THROW_OOP_(linkage_exception(), NULL); // throws exception and returns
  1747     THROW_OOP_(linkage_exception(), NULL); // throws exception and returns
  1768   }
  1748   }
  1769 
  1749 
  1770   return k;
  1750   return k;
  1771 }
  1751 }
       
  1752 
  1772 Handle SystemDictionary::compute_loader_lock_object(Handle class_loader, TRAPS) {
  1753 Handle SystemDictionary::compute_loader_lock_object(Handle class_loader, TRAPS) {
  1773   // If class_loader is NULL we synchronize on _system_loader_lock_obj
  1754   // If class_loader is NULL we synchronize on _system_loader_lock_obj
  1774   if (class_loader.is_null()) {
  1755   if (class_loader.is_null()) {
  1775     return Handle(THREAD, _system_loader_lock_obj);
  1756     return Handle(THREAD, _system_loader_lock_obj);
  1776   } else {
  1757   } else {
  1804 // ----------------------------------------------------------------------------
  1785 // ----------------------------------------------------------------------------
  1805 // Lookup
  1786 // Lookup
  1806 
  1787 
  1807 InstanceKlass* SystemDictionary::find_class(int index, unsigned int hash,
  1788 InstanceKlass* SystemDictionary::find_class(int index, unsigned int hash,
  1808                                             Symbol* class_name,
  1789                                             Symbol* class_name,
  1809                                             ClassLoaderData* loader_data) {
  1790                                             Dictionary* dictionary) {
  1810   assert_locked_or_safepoint(SystemDictionary_lock);
  1791   assert_locked_or_safepoint(SystemDictionary_lock);
  1811   assert (index == dictionary()->index_for(class_name, loader_data),
  1792   return dictionary->find_class(index, hash, class_name);
  1812           "incorrect index?");
       
  1813 
       
  1814   return dictionary()->find_class(index, hash, class_name, loader_data);
       
  1815 }
  1793 }
  1816 
  1794 
  1817 
  1795 
  1818 // Basic find on classes in the midst of being loaded
  1796 // Basic find on classes in the midst of being loaded
  1819 Symbol* SystemDictionary::find_placeholder(Symbol* class_name,
  1797 Symbol* SystemDictionary::find_placeholder(Symbol* class_name,
  1820                                            ClassLoaderData* loader_data) {
  1798                                            ClassLoaderData* loader_data) {
  1821   assert_locked_or_safepoint(SystemDictionary_lock);
  1799   assert_locked_or_safepoint(SystemDictionary_lock);
  1822   unsigned int p_hash = placeholders()->compute_hash(class_name, loader_data);
  1800   unsigned int p_hash = placeholders()->compute_hash(class_name);
  1823   int p_index = placeholders()->hash_to_index(p_hash);
  1801   int p_index = placeholders()->hash_to_index(p_hash);
  1824   return placeholders()->find_entry(p_index, p_hash, class_name, loader_data);
  1802   return placeholders()->find_entry(p_index, p_hash, class_name, loader_data);
  1825 }
  1803 }
  1826 
  1804 
  1827 
  1805 
  1828 // Used for assertions and verification only
  1806 // Used for assertions and verification only
       
  1807 // Precalculating the hash and index is an optimization because there are many lookups
       
  1808 // before adding the class.
  1829 InstanceKlass* SystemDictionary::find_class(Symbol* class_name, ClassLoaderData* loader_data) {
  1809 InstanceKlass* SystemDictionary::find_class(Symbol* class_name, ClassLoaderData* loader_data) {
       
  1810   assert_locked_or_safepoint(SystemDictionary_lock);
  1830   #ifndef ASSERT
  1811   #ifndef ASSERT
  1831   guarantee(VerifyBeforeGC      ||
  1812   guarantee(VerifyBeforeGC      ||
  1832             VerifyDuringGC      ||
  1813             VerifyDuringGC      ||
  1833             VerifyBeforeExit    ||
  1814             VerifyBeforeExit    ||
  1834             VerifyDuringStartup ||
  1815             VerifyDuringStartup ||
  1835             VerifyAfterGC, "too expensive");
  1816             VerifyAfterGC, "too expensive");
  1836   #endif
  1817   #endif
  1837   assert_locked_or_safepoint(SystemDictionary_lock);
  1818 
  1838 
  1819   Dictionary* dictionary = loader_data->dictionary();
  1839   // First look in the loaded class array
  1820   unsigned int d_hash = dictionary->compute_hash(class_name);
  1840   unsigned int d_hash = dictionary()->compute_hash(class_name, loader_data);
  1821   int d_index = dictionary->hash_to_index(d_hash);
  1841   int d_index = dictionary()->hash_to_index(d_hash);
  1822   return find_class(d_index, d_hash, class_name, dictionary);
  1842   return find_class(d_index, d_hash, class_name, loader_data);
       
  1843 }
       
  1844 
       
  1845 
       
  1846 // Get the next class in the dictionary.
       
  1847 Klass* SystemDictionary::try_get_next_class() {
       
  1848   return dictionary()->try_get_next_class();
       
  1849 }
  1823 }
  1850 
  1824 
  1851 
  1825 
  1852 // ----------------------------------------------------------------------------
  1826 // ----------------------------------------------------------------------------
  1853 // Update hierachy. This is done before the new klass has been added to the SystemDictionary. The Recompile_lock
  1827 // Update hierachy. This is done before the new klass has been added to the SystemDictionary. The Recompile_lock
  1870 }
  1844 }
  1871 
  1845 
  1872 // ----------------------------------------------------------------------------
  1846 // ----------------------------------------------------------------------------
  1873 // GC support
  1847 // GC support
  1874 
  1848 
  1875 // Following roots during mark-sweep is separated in two phases.
       
  1876 //
       
  1877 // The first phase follows preloaded classes and all other system
       
  1878 // classes, since these will never get unloaded anyway.
       
  1879 //
       
  1880 // The second phase removes (unloads) unreachable classes from the
       
  1881 // system dictionary and follows the remaining classes' contents.
       
  1882 
       
  1883 void SystemDictionary::always_strong_oops_do(OopClosure* blk) {
  1849 void SystemDictionary::always_strong_oops_do(OopClosure* blk) {
  1884   roots_oops_do(blk, NULL);
  1850   roots_oops_do(blk, NULL);
  1885 }
  1851 }
  1886 
  1852 
  1887 // Calculate a "good" systemdictionary size based
       
  1888 // on predicted or current loaded classes count
       
  1889 int SystemDictionary::calculate_systemdictionary_size(int classcount) {
       
  1890   int newsize = _old_default_sdsize;
       
  1891   if ((classcount > 0)  && !DumpSharedSpaces) {
       
  1892     int desiredsize = classcount/_average_depth_goal;
       
  1893     for (newsize = _primelist[_sdgeneration]; _sdgeneration < _prime_array_size -1;
       
  1894          newsize = _primelist[++_sdgeneration]) {
       
  1895       if (desiredsize <=  newsize) {
       
  1896         break;
       
  1897       }
       
  1898     }
       
  1899   }
       
  1900   return newsize;
       
  1901 }
       
  1902 
  1853 
  1903 #ifdef ASSERT
  1854 #ifdef ASSERT
  1904 class VerifySDReachableAndLiveClosure : public OopClosure {
  1855 class VerifySDReachableAndLiveClosure : public OopClosure {
  1905 private:
  1856 private:
  1906   BoolObjectClosure* _is_alive;
  1857   BoolObjectClosure* _is_alive;
  1907 
  1858 
  1908   template <class T> void do_oop_work(T* p) {
  1859   template <class T> void do_oop_work(T* p) {
  1909     oop obj = oopDesc::load_decode_heap_oop(p);
  1860     oop obj = oopDesc::load_decode_heap_oop(p);
  1910     guarantee(_is_alive->do_object_b(obj), "Oop in system dictionary must be live");
  1861     guarantee(_is_alive->do_object_b(obj), "Oop in protection domain cache table must be live");
  1911   }
  1862   }
  1912 
  1863 
  1913 public:
  1864 public:
  1914   VerifySDReachableAndLiveClosure(BoolObjectClosure* is_alive) : OopClosure(), _is_alive(is_alive) { }
  1865   VerifySDReachableAndLiveClosure(BoolObjectClosure* is_alive) : OopClosure(), _is_alive(is_alive) { }
  1915 
  1866 
  1934                                                             do_cleaning);
  1885                                                             do_cleaning);
  1935   }
  1886   }
  1936 
  1887 
  1937   if (unloading_occurred) {
  1888   if (unloading_occurred) {
  1938     GCTraceTime(Debug, gc, phases) t("Dictionary", gc_timer);
  1889     GCTraceTime(Debug, gc, phases) t("Dictionary", gc_timer);
  1939     dictionary()->do_unloading();
       
  1940     constraints()->purge_loader_constraints();
  1890     constraints()->purge_loader_constraints();
  1941     resolution_errors()->purge_resolution_errors();
  1891     resolution_errors()->purge_resolution_errors();
  1942   }
  1892   }
  1943 
  1893 
  1944   {
  1894   {
  1945     GCTraceTime(Debug, gc, phases) t("ProtectionDomainCacheTable", gc_timer);
  1895     GCTraceTime(Debug, gc, phases) t("ProtectionDomainCacheTable", gc_timer);
  1946     // Oops referenced by the system dictionary may get unreachable independently
  1896     // Oops referenced by the protection domain cache table may get unreachable independently
  1947     // of the class loader (eg. cached protection domain oops). So we need to
  1897     // of the class loader (eg. cached protection domain oops). So we need to
  1948     // explicitly unlink them here instead of in Dictionary::do_unloading.
  1898     // explicitly unlink them here.
  1949     dictionary()->unlink(is_alive);
  1899     _pd_cache_table->unlink(is_alive);
       
  1900 
  1950 #ifdef ASSERT
  1901 #ifdef ASSERT
  1951     VerifySDReachableAndLiveClosure cl(is_alive);
  1902     VerifySDReachableAndLiveClosure cl(is_alive);
  1952     dictionary()->oops_do(&cl);
  1903     _pd_cache_table->oops_do(&cl);
  1953 #endif
  1904 #endif
  1954   }
  1905   }
  1955 
  1906 
  1956   if (do_cleaning) {
  1907   if (do_cleaning) {
  1957     GCTraceTime(Debug, gc, phases) t("ResolvedMethodTable", gc_timer);
  1908     GCTraceTime(Debug, gc, phases) t("ResolvedMethodTable", gc_timer);
  1964 void SystemDictionary::roots_oops_do(OopClosure* strong, OopClosure* weak) {
  1915 void SystemDictionary::roots_oops_do(OopClosure* strong, OopClosure* weak) {
  1965   strong->do_oop(&_java_system_loader);
  1916   strong->do_oop(&_java_system_loader);
  1966   strong->do_oop(&_system_loader_lock_obj);
  1917   strong->do_oop(&_system_loader_lock_obj);
  1967   CDS_ONLY(SystemDictionaryShared::roots_oops_do(strong);)
  1918   CDS_ONLY(SystemDictionaryShared::roots_oops_do(strong);)
  1968 
  1919 
  1969   // Adjust dictionary
  1920   // Do strong roots marking if the closures are the same.
  1970   dictionary()->roots_oops_do(strong, weak);
  1921   if (strong == weak || !ClassUnloading) {
       
  1922     // Only the protection domain oops contain references into the heap. Iterate
       
  1923     // over all of them.
       
  1924     _pd_cache_table->oops_do(strong);
       
  1925   } else {
       
  1926    if (weak != NULL) {
       
  1927      _pd_cache_table->oops_do(weak);
       
  1928    }
       
  1929   }
  1971 
  1930 
  1972   // Visit extra methods
  1931   // Visit extra methods
  1973   invoke_method_table()->oops_do(strong);
  1932   invoke_method_table()->oops_do(strong);
  1974 
  1933 
  1975   if (weak != NULL) {
  1934   if (weak != NULL) {
  1980 void SystemDictionary::oops_do(OopClosure* f) {
  1939 void SystemDictionary::oops_do(OopClosure* f) {
  1981   f->do_oop(&_java_system_loader);
  1940   f->do_oop(&_java_system_loader);
  1982   f->do_oop(&_system_loader_lock_obj);
  1941   f->do_oop(&_system_loader_lock_obj);
  1983   CDS_ONLY(SystemDictionaryShared::oops_do(f);)
  1942   CDS_ONLY(SystemDictionaryShared::oops_do(f);)
  1984 
  1943 
  1985   // Adjust dictionary
  1944   // Only the protection domain oops contain references into the heap. Iterate
  1986   dictionary()->oops_do(f);
  1945   // over all of them.
       
  1946   _pd_cache_table->oops_do(f);
  1987 
  1947 
  1988   // Visit extra methods
  1948   // Visit extra methods
  1989   invoke_method_table()->oops_do(f);
  1949   invoke_method_table()->oops_do(f);
  1990 
  1950 
  1991   ResolvedMethodTable::oops_do(f);
  1951   ResolvedMethodTable::oops_do(f);
  1992 }
       
  1993 
       
  1994 // Just the classes from defining class loaders
       
  1995 // Don't iterate over placeholders
       
  1996 void SystemDictionary::classes_do(void f(Klass*)) {
       
  1997   dictionary()->classes_do(f);
       
  1998 }
       
  1999 
       
  2000 // Added for initialize_itable_for_klass
       
  2001 //   Just the classes from defining class loaders
       
  2002 // Don't iterate over placeholders
       
  2003 void SystemDictionary::classes_do(void f(Klass*, TRAPS), TRAPS) {
       
  2004   dictionary()->classes_do(f, CHECK);
       
  2005 }
       
  2006 
       
  2007 //   All classes, and their class loaders
       
  2008 // Don't iterate over placeholders
       
  2009 void SystemDictionary::classes_do(void f(Klass*, ClassLoaderData*)) {
       
  2010   dictionary()->classes_do(f);
       
  2011 }
  1952 }
  2012 
  1953 
  2013 void SystemDictionary::methods_do(void f(Method*)) {
  1954 void SystemDictionary::methods_do(void f(Method*)) {
  2014   // Walk methods in loaded classes
  1955   // Walk methods in loaded classes
  2015   ClassLoaderDataGraph::methods_do(f);
  1956   ClassLoaderDataGraph::methods_do(f);
  2016   // Walk method handle intrinsics
  1957   // Walk method handle intrinsics
  2017   invoke_method_table()->methods_do(f);
  1958   invoke_method_table()->methods_do(f);
  2018 }
  1959 }
  2019 
  1960 
  2020 void SystemDictionary::remove_classes_in_error_state() {
  1961 void SystemDictionary::remove_classes_in_error_state() {
  2021   dictionary()->remove_classes_in_error_state();
  1962   ClassLoaderData::the_null_class_loader_data()->dictionary()->remove_classes_in_error_state();
  2022 }
  1963 }
  2023 
  1964 
  2024 // ----------------------------------------------------------------------------
  1965 // ----------------------------------------------------------------------------
  2025 // Lazily load klasses
  1966 // Lazily load klasses
  2026 
  1967 
  2040 // ----------------------------------------------------------------------------
  1981 // ----------------------------------------------------------------------------
  2041 // Initialization
  1982 // Initialization
  2042 
  1983 
  2043 void SystemDictionary::initialize(TRAPS) {
  1984 void SystemDictionary::initialize(TRAPS) {
  2044   // Allocate arrays
  1985   // Allocate arrays
  2045   assert(dictionary() == NULL,
  1986   _placeholders        = new PlaceholderTable(_placeholder_table_size);
  2046          "SystemDictionary should only be initialized once");
       
  2047   _sdgeneration        = 0;
       
  2048   _dictionary          = new Dictionary(calculate_systemdictionary_size(PredictedLoadedClassCount));
       
  2049   _placeholders        = new PlaceholderTable(_nof_buckets);
       
  2050   _number_of_modifications = 0;
  1987   _number_of_modifications = 0;
  2051   _loader_constraints  = new LoaderConstraintTable(_loader_constraint_size);
  1988   _loader_constraints  = new LoaderConstraintTable(_loader_constraint_size);
  2052   _resolution_errors   = new ResolutionErrorTable(_resolution_error_size);
  1989   _resolution_errors   = new ResolutionErrorTable(_resolution_error_size);
  2053   _invoke_method_table = new SymbolPropertyTable(_invoke_method_size);
  1990   _invoke_method_table = new SymbolPropertyTable(_invoke_method_size);
       
  1991   _pd_cache_table = new ProtectionDomainCacheTable(defaultProtectionDomainCacheSize);
  2054 
  1992 
  2055   // Allocate private object used as system class loader lock
  1993   // Allocate private object used as system class loader lock
  2056   _system_loader_lock_obj = oopFactory::new_intArray(0, CHECK);
  1994   _system_loader_lock_obj = oopFactory::new_intArray(0, CHECK);
  2057   // Initialize basic classes
  1995   // Initialize basic classes
  2058   initialize_preloaded_classes(CHECK);
  1996   initialize_preloaded_classes(CHECK);
  2202 }
  2140 }
  2203 
  2141 
  2204 // Constraints on class loaders. The details of the algorithm can be
  2142 // Constraints on class loaders. The details of the algorithm can be
  2205 // found in the OOPSLA'98 paper "Dynamic Class Loading in the Java
  2143 // found in the OOPSLA'98 paper "Dynamic Class Loading in the Java
  2206 // Virtual Machine" by Sheng Liang and Gilad Bracha.  The basic idea is
  2144 // Virtual Machine" by Sheng Liang and Gilad Bracha.  The basic idea is
  2207 // that the system dictionary needs to maintain a set of contraints that
  2145 // that the dictionary needs to maintain a set of contraints that
  2208 // must be satisfied by all classes in the dictionary.
  2146 // must be satisfied by all classes in the dictionary.
  2209 // if defining is true, then LinkageError if already in systemDictionary
  2147 // if defining is true, then LinkageError if already in dictionary
  2210 // if initiating loader, then ok if InstanceKlass matches existing entry
  2148 // if initiating loader, then ok if InstanceKlass matches existing entry
  2211 
  2149 
  2212 void SystemDictionary::check_constraints(int d_index, unsigned int d_hash,
  2150 void SystemDictionary::check_constraints(int d_index, unsigned int d_hash,
  2213                                          InstanceKlass* k,
  2151                                          InstanceKlass* k,
  2214                                          Handle class_loader, bool defining,
  2152                                          Handle class_loader, bool defining,
  2219     Symbol*  name  = k->name();
  2157     Symbol*  name  = k->name();
  2220     ClassLoaderData *loader_data = class_loader_data(class_loader);
  2158     ClassLoaderData *loader_data = class_loader_data(class_loader);
  2221 
  2159 
  2222     MutexLocker mu(SystemDictionary_lock, THREAD);
  2160     MutexLocker mu(SystemDictionary_lock, THREAD);
  2223 
  2161 
  2224     InstanceKlass* check = find_class(d_index, d_hash, name, loader_data);
  2162     InstanceKlass* check = find_class(d_index, d_hash, name, loader_data->dictionary());
  2225     if (check != NULL) {
  2163     if (check != NULL) {
  2226       // if different InstanceKlass - duplicate class definition,
  2164       // if different InstanceKlass - duplicate class definition,
  2227       // else - ok, class loaded by a different thread in parallel,
  2165       // else - ok, class loaded by a different thread in parallel,
  2228       // we should only have found it if it was done loading and ok to use
  2166       // we should only have found it if it was done loading and ok to use
  2229       // system dictionary only holds instance classes, placeholders
  2167       // dictionary only holds instance classes, placeholders
  2230       // also holds array classes
  2168       // also holds array classes
  2231 
  2169 
  2232       assert(check->is_instance_klass(), "noninstance in systemdictionary");
  2170       assert(check->is_instance_klass(), "noninstance in systemdictionary");
  2233       if ((defining == true) || (k != check)) {
  2171       if ((defining == true) || (k != check)) {
  2234         linkage_error1 = "loader (instance of ";
  2172         linkage_error1 = "loader (instance of ";
  2265     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
  2203     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
  2266   }
  2204   }
  2267 }
  2205 }
  2268 
  2206 
  2269 
  2207 
  2270 // Update system dictionary - done after check_constraint and add_to_hierachy
  2208 // Update class loader data dictionary - done after check_constraint and add_to_hierachy
  2271 // have been called.
  2209 // have been called.
  2272 void SystemDictionary::update_dictionary(int d_index, unsigned int d_hash,
  2210 void SystemDictionary::update_dictionary(int d_index, unsigned int d_hash,
  2273                                          int p_index, unsigned int p_hash,
  2211                                          int p_index, unsigned int p_hash,
  2274                                          InstanceKlass* k,
  2212                                          InstanceKlass* k,
  2275                                          Handle class_loader,
  2213                                          Handle class_loader,
  2278   assert_locked_or_safepoint(Compile_lock);
  2216   assert_locked_or_safepoint(Compile_lock);
  2279   Symbol*  name  = k->name();
  2217   Symbol*  name  = k->name();
  2280   ClassLoaderData *loader_data = class_loader_data(class_loader);
  2218   ClassLoaderData *loader_data = class_loader_data(class_loader);
  2281 
  2219 
  2282   {
  2220   {
  2283   MutexLocker mu1(SystemDictionary_lock, THREAD);
  2221     MutexLocker mu1(SystemDictionary_lock, THREAD);
  2284 
  2222 
  2285   // See whether biased locking is enabled and if so set it for this
  2223     // See whether biased locking is enabled and if so set it for this
  2286   // klass.
  2224     // klass.
  2287   // Note that this must be done past the last potential blocking
  2225     // Note that this must be done past the last potential blocking
  2288   // point / safepoint. We enable biased locking lazily using a
  2226     // point / safepoint. We enable biased locking lazily using a
  2289   // VM_Operation to iterate the SystemDictionary and installing the
  2227     // VM_Operation to iterate the SystemDictionary and installing the
  2290   // biasable mark word into each InstanceKlass's prototype header.
  2228     // biasable mark word into each InstanceKlass's prototype header.
  2291   // To avoid race conditions where we accidentally miss enabling the
  2229     // To avoid race conditions where we accidentally miss enabling the
  2292   // optimization for one class in the process of being added to the
  2230     // optimization for one class in the process of being added to the
  2293   // dictionary, we must not safepoint after the test of
  2231     // dictionary, we must not safepoint after the test of
  2294   // BiasedLocking::enabled().
  2232     // BiasedLocking::enabled().
  2295   if (UseBiasedLocking && BiasedLocking::enabled()) {
  2233     if (UseBiasedLocking && BiasedLocking::enabled()) {
  2296     // Set biased locking bit for all loaded classes; it will be
  2234       // Set biased locking bit for all loaded classes; it will be
  2297     // cleared if revocation occurs too often for this type
  2235       // cleared if revocation occurs too often for this type
  2298     // NOTE that we must only do this when the class is initally
  2236       // NOTE that we must only do this when the class is initally
  2299     // defined, not each time it is referenced from a new class loader
  2237       // defined, not each time it is referenced from a new class loader
  2300     if (k->class_loader() == class_loader()) {
  2238       if (k->class_loader() == class_loader()) {
  2301       k->set_prototype_header(markOopDesc::biased_locking_prototype());
  2239         k->set_prototype_header(markOopDesc::biased_locking_prototype());
  2302     }
  2240       }
  2303   }
  2241     }
  2304 
  2242 
  2305   // Make a new system dictionary entry.
  2243     // Make a new dictionary entry.
  2306   InstanceKlass* sd_check = find_class(d_index, d_hash, name, loader_data);
  2244     Dictionary* dictionary = loader_data->dictionary();
  2307   if (sd_check == NULL) {
  2245     InstanceKlass* sd_check = find_class(d_index, d_hash, name, dictionary);
  2308     dictionary()->add_klass(name, loader_data, k);
  2246     if (sd_check == NULL) {
  2309     notice_modification();
  2247       dictionary->add_klass(d_index, d_hash, name, k);
  2310   }
  2248       notice_modification();
  2311 #ifdef ASSERT
  2249     }
  2312   sd_check = find_class(d_index, d_hash, name, loader_data);
  2250   #ifdef ASSERT
  2313   assert (sd_check != NULL, "should have entry in system dictionary");
  2251     sd_check = find_class(d_index, d_hash, name, dictionary);
  2314   // Note: there may be a placeholder entry: for circularity testing
  2252     assert (sd_check != NULL, "should have entry in dictionary");
  2315   // or for parallel defines
  2253     // Note: there may be a placeholder entry: for circularity testing
  2316 #endif
  2254     // or for parallel defines
       
  2255   #endif
  2317     SystemDictionary_lock->notify_all();
  2256     SystemDictionary_lock->notify_all();
  2318   }
  2257   }
  2319 }
  2258 }
  2320 
  2259 
  2321 
  2260 
  2381       return true;
  2320       return true;
  2382     } else {
  2321     } else {
  2383       constraint_name = fd.object_key();
  2322       constraint_name = fd.object_key();
  2384     }
  2323     }
  2385   }
  2324   }
  2386   unsigned int d_hash1 = dictionary()->compute_hash(constraint_name, loader_data1);
  2325 
  2387   int d_index1 = dictionary()->hash_to_index(d_hash1);
  2326   Dictionary* dictionary1 = loader_data1->dictionary();
  2388 
  2327   unsigned int d_hash1 = dictionary1->compute_hash(constraint_name);
  2389   unsigned int d_hash2 = dictionary()->compute_hash(constraint_name, loader_data2);
  2328   int d_index1 = dictionary1->hash_to_index(d_hash1);
  2390   int d_index2 = dictionary()->hash_to_index(d_hash2);
  2329 
       
  2330   Dictionary* dictionary2 = loader_data2->dictionary();
       
  2331   unsigned int d_hash2 = dictionary2->compute_hash(constraint_name);
       
  2332   int d_index2 = dictionary2->hash_to_index(d_hash2);
       
  2333 
  2391   {
  2334   {
  2392   MutexLocker mu_s(SystemDictionary_lock, THREAD);
  2335     MutexLocker mu_s(SystemDictionary_lock, THREAD);
  2393 
  2336     InstanceKlass* klass1 = find_class(d_index1, d_hash1, constraint_name, dictionary1);
  2394   // Better never do a GC while we're holding these oops
  2337     InstanceKlass* klass2 = find_class(d_index2, d_hash2, constraint_name, dictionary2);
  2395   NoSafepointVerifier nosafepoint;
  2338     return constraints()->add_entry(constraint_name, klass1, class_loader1,
  2396 
  2339                                     klass2, class_loader2);
  2397   InstanceKlass* klass1 = find_class(d_index1, d_hash1, constraint_name, loader_data1);
       
  2398   InstanceKlass* klass2 = find_class(d_index2, d_hash2, constraint_name, loader_data2);
       
  2399   return constraints()->add_entry(constraint_name, klass1, class_loader1,
       
  2400                                   klass2, class_loader2);
       
  2401   }
  2340   }
  2402 }
  2341 }
  2403 
  2342 
  2404 // Add entry to resolution error table to record the error when the first
  2343 // Add entry to resolution error table to record the error when the first
  2405 // attempt to resolve a reference to a class has failed.
  2344 // attempt to resolve a reference to a class has failed.
  2845   Handle mname(THREAD, (oop) result.get_jobject());
  2784   Handle mname(THREAD, (oop) result.get_jobject());
  2846   (*method_type_result) = method_type;
  2785   (*method_type_result) = method_type;
  2847   return unpack_method_and_appendix(mname, caller, appendix_box, appendix_result, THREAD);
  2786   return unpack_method_and_appendix(mname, caller, appendix_box, appendix_result, THREAD);
  2848 }
  2787 }
  2849 
  2788 
  2850 // Since the identity hash code for symbols changes when the symbols are
  2789 // Protection domain cache table handling
  2851 // moved from the regular perm gen (hash in the mark word) to the shared
  2790 
  2852 // spaces (hash is the address), the classes loaded into the dictionary
  2791 ProtectionDomainCacheEntry* SystemDictionary::cache_get(Handle protection_domain) {
  2853 // may be in the wrong buckets.
  2792   return _pd_cache_table->get(protection_domain);
       
  2793 }
       
  2794 
  2854 
  2795 
  2855 void SystemDictionary::reorder_dictionary() {
  2796 void SystemDictionary::reorder_dictionary() {
  2856   dictionary()->reorder_dictionary();
  2797   ClassLoaderData::the_null_class_loader_data()->dictionary()->reorder_dictionary();
  2857 }
  2798 }
  2858 
  2799 
  2859 
  2800 
  2860 void SystemDictionary::copy_buckets(char** top, char* end) {
  2801 void SystemDictionary::copy_buckets(char** top, char* end) {
  2861   dictionary()->copy_buckets(top, end);
  2802   ClassLoaderData::the_null_class_loader_data()->dictionary()->copy_buckets(top, end);
  2862 }
  2803 }
  2863 
  2804 
  2864 
  2805 
  2865 void SystemDictionary::copy_table(char** top, char* end) {
  2806 void SystemDictionary::copy_table(char** top, char* end) {
  2866   dictionary()->copy_table(top, end);
  2807   ClassLoaderData::the_null_class_loader_data()->dictionary()->copy_table(top, end);
  2867 }
  2808 }
  2868 
       
  2869 int SystemDictionary::number_of_classes() {
       
  2870   return dictionary()->number_of_entries();
       
  2871 }
       
  2872 
       
  2873 
  2809 
  2874 // ----------------------------------------------------------------------------
  2810 // ----------------------------------------------------------------------------
  2875 void SystemDictionary::print_shared(bool details) {
  2811 void SystemDictionary::print_shared(bool details) {
  2876   shared_dictionary()->print(details);
  2812   shared_dictionary()->print(details);
  2877 }
  2813 }
  2878 
  2814 
  2879 void SystemDictionary::print(bool details) {
  2815 void SystemDictionary::print(bool details) {
  2880   dictionary()->print(details);
  2816   if (shared_dictionary() != NULL) {
       
  2817     tty->print_cr("Shared Dictionary");
       
  2818     shared_dictionary()->print(details);
       
  2819   }
       
  2820 
       
  2821   GCMutexLocker mu(SystemDictionary_lock);
       
  2822 
       
  2823   ClassLoaderDataGraph::print_dictionary(details);
  2881 
  2824 
  2882   // Placeholders
  2825   // Placeholders
  2883   GCMutexLocker mu(SystemDictionary_lock);
       
  2884   placeholders()->print();
  2826   placeholders()->print();
       
  2827   tty->cr();
  2885 
  2828 
  2886   // loader constraints - print under SD_lock
  2829   // loader constraints - print under SD_lock
  2887   constraints()->print();
  2830   constraints()->print();
       
  2831   tty->cr();
       
  2832 
       
  2833   _pd_cache_table->print();
       
  2834   tty->cr();
  2888 }
  2835 }
  2889 
  2836 
  2890 
  2837 
  2891 void SystemDictionary::verify() {
  2838 void SystemDictionary::verify() {
  2892   guarantee(dictionary() != NULL, "Verify of system dictionary failed");
       
  2893   guarantee(constraints() != NULL,
  2839   guarantee(constraints() != NULL,
  2894             "Verify of loader constraints failed");
  2840             "Verify of loader constraints failed");
  2895   guarantee(dictionary()->number_of_entries() >= 0 &&
  2841   guarantee(placeholders()->number_of_entries() >= 0,
  2896             placeholders()->number_of_entries() >= 0,
  2842             "Verify of placeholders failed");
  2897             "Verify of system dictionary failed");
  2843 
       
  2844   GCMutexLocker mu(SystemDictionary_lock);
  2898 
  2845 
  2899   // Verify dictionary
  2846   // Verify dictionary
  2900   dictionary()->verify();
  2847   ClassLoaderDataGraph::verify_dictionary();
  2901 
  2848 
  2902   GCMutexLocker mu(SystemDictionary_lock);
       
  2903   placeholders()->verify();
  2849   placeholders()->verify();
  2904 
  2850 
  2905   // Verify constraint table
  2851   // Verify constraint table
  2906   guarantee(constraints() != NULL, "Verify of loader constraints failed");
  2852   guarantee(constraints() != NULL, "Verify of loader constraints failed");
  2907   constraints()->verify(dictionary(), placeholders());
  2853   constraints()->verify(placeholders());
       
  2854 
       
  2855   _pd_cache_table->verify();
  2908 }
  2856 }
  2909 
  2857 
  2910 // caller needs ResourceMark
  2858 // caller needs ResourceMark
  2911 const char* SystemDictionary::loader_name(const oop loader) {
  2859 const char* SystemDictionary::loader_name(const oop loader) {
  2912   return ((loader) == NULL ? "<bootloader>" :
  2860   return ((loader) == NULL ? "<bootloader>" :