hotspot/src/share/vm/classfile/systemDictionary.cpp
changeset 13728 882756847a04
parent 13391 30245956af37
child 13736 5b15a8f57979
equal deleted inserted replaced
13727:caf5eb7dd4a7 13728:882756847a04
    21  * questions.
    21  * questions.
    22  *
    22  *
    23  */
    23  */
    24 
    24 
    25 #include "precompiled.hpp"
    25 #include "precompiled.hpp"
       
    26 #include "classfile/classLoaderData.inline.hpp"
    26 #include "classfile/dictionary.hpp"
    27 #include "classfile/dictionary.hpp"
    27 #include "classfile/javaClasses.hpp"
    28 #include "classfile/javaClasses.hpp"
    28 #include "classfile/loaderConstraints.hpp"
    29 #include "classfile/loaderConstraints.hpp"
    29 #include "classfile/placeholders.hpp"
    30 #include "classfile/placeholders.hpp"
    30 #include "classfile/resolutionErrors.hpp"
    31 #include "classfile/resolutionErrors.hpp"
    36 #include "memory/gcLocker.hpp"
    37 #include "memory/gcLocker.hpp"
    37 #include "memory/oopFactory.hpp"
    38 #include "memory/oopFactory.hpp"
    38 #include "oops/instanceKlass.hpp"
    39 #include "oops/instanceKlass.hpp"
    39 #include "oops/instanceRefKlass.hpp"
    40 #include "oops/instanceRefKlass.hpp"
    40 #include "oops/klass.inline.hpp"
    41 #include "oops/klass.inline.hpp"
    41 #include "oops/methodDataOop.hpp"
    42 #include "oops/methodData.hpp"
    42 #include "oops/objArrayKlass.hpp"
    43 #include "oops/objArrayKlass.hpp"
    43 #include "oops/oop.inline.hpp"
    44 #include "oops/oop.inline.hpp"
    44 #include "oops/oop.inline2.hpp"
    45 #include "oops/oop.inline2.hpp"
    45 #include "oops/typeArrayKlass.hpp"
    46 #include "oops/typeArrayKlass.hpp"
    46 #include "prims/jvmtiEnvBase.hpp"
    47 #include "prims/jvmtiEnvBase.hpp"
    69 const int   SystemDictionary::_primelist[_prime_array_size] = {1009,2017,4049,5051,10103,
    70 const int   SystemDictionary::_primelist[_prime_array_size] = {1009,2017,4049,5051,10103,
    70               20201,40423,99991};
    71               20201,40423,99991};
    71 
    72 
    72 oop         SystemDictionary::_system_loader_lock_obj     =  NULL;
    73 oop         SystemDictionary::_system_loader_lock_obj     =  NULL;
    73 
    74 
    74 klassOop    SystemDictionary::_well_known_klasses[SystemDictionary::WKID_LIMIT]
    75 Klass*      SystemDictionary::_well_known_klasses[SystemDictionary::WKID_LIMIT]
    75                                                           =  { NULL /*, NULL...*/ };
    76                                                           =  { NULL /*, NULL...*/ };
    76 
    77 
    77 klassOop    SystemDictionary::_box_klasses[T_VOID+1]      =  { NULL /*, NULL...*/ };
    78 Klass*      SystemDictionary::_box_klasses[T_VOID+1]      =  { NULL /*, NULL...*/ };
    78 
    79 
    79 oop         SystemDictionary::_java_system_loader         =  NULL;
    80 oop         SystemDictionary::_java_system_loader         =  NULL;
    80 
    81 
    81 bool        SystemDictionary::_has_loadClassInternal      =  false;
    82 bool        SystemDictionary::_has_loadClassInternal      =  false;
    82 bool        SystemDictionary::_has_checkPackageAccess     =  false;
    83 bool        SystemDictionary::_has_checkPackageAccess     =  false;
    83 
    84 
    84 // lazily initialized klass variables
    85 // lazily initialized klass variables
    85 volatile klassOop    SystemDictionary::_abstract_ownable_synchronizer_klass = NULL;
    86 Klass* volatile SystemDictionary::_abstract_ownable_synchronizer_klass = NULL;
    86 
    87 
    87 
    88 
    88 // ----------------------------------------------------------------------------
    89 // ----------------------------------------------------------------------------
    89 // Java-level SystemLoader
    90 // Java-level SystemLoader
    90 
    91 
   103 
   104 
   104   _java_system_loader = (oop)result.get_jobject();
   105   _java_system_loader = (oop)result.get_jobject();
   105 }
   106 }
   106 
   107 
   107 
   108 
       
   109 ClassLoaderData* SystemDictionary::register_loader(Handle class_loader) {
       
   110   if (class_loader() == NULL) return ClassLoaderData::the_null_class_loader_data();
       
   111   return ClassLoaderDataGraph::find_or_create(class_loader);
       
   112 }
       
   113 
   108 // ----------------------------------------------------------------------------
   114 // ----------------------------------------------------------------------------
   109 // debugging
   115 // debugging
   110 
   116 
   111 #ifdef ASSERT
   117 #ifdef ASSERT
   112 
   118 
   143 // ----------------------------------------------------------------------------
   149 // ----------------------------------------------------------------------------
   144 // Resolving of classes
   150 // Resolving of classes
   145 
   151 
   146 // Forwards to resolve_or_null
   152 // Forwards to resolve_or_null
   147 
   153 
   148 klassOop SystemDictionary::resolve_or_fail(Symbol* class_name, Handle class_loader, Handle protection_domain, bool throw_error, TRAPS) {
   154 Klass* SystemDictionary::resolve_or_fail(Symbol* class_name, Handle class_loader, Handle protection_domain, bool throw_error, TRAPS) {
   149   klassOop klass = resolve_or_null(class_name, class_loader, protection_domain, THREAD);
   155   Klass* klass = resolve_or_null(class_name, class_loader, protection_domain, THREAD);
   150   if (HAS_PENDING_EXCEPTION || klass == NULL) {
   156   if (HAS_PENDING_EXCEPTION || klass == NULL) {
   151     KlassHandle k_h(THREAD, klass);
   157     KlassHandle k_h(THREAD, klass);
   152     // can return a null klass
   158     // can return a null klass
   153     klass = handle_resolution_exception(class_name, class_loader, protection_domain, throw_error, k_h, THREAD);
   159     klass = handle_resolution_exception(class_name, class_loader, protection_domain, throw_error, k_h, THREAD);
   154   }
   160   }
   155   return klass;
   161   return klass;
   156 }
   162 }
   157 
   163 
   158 klassOop SystemDictionary::handle_resolution_exception(Symbol* class_name, Handle class_loader, Handle protection_domain, bool throw_error, KlassHandle klass_h, TRAPS) {
   164 Klass* SystemDictionary::handle_resolution_exception(Symbol* class_name, Handle class_loader, Handle protection_domain, bool throw_error, KlassHandle klass_h, TRAPS) {
   159   if (HAS_PENDING_EXCEPTION) {
   165   if (HAS_PENDING_EXCEPTION) {
   160     // If we have a pending exception we forward it to the caller, unless throw_error is true,
   166     // If we have a pending exception we forward it to the caller, unless throw_error is true,
   161     // in which case we have to check whether the pending exception is a ClassNotFoundException,
   167     // in which case we have to check whether the pending exception is a ClassNotFoundException,
   162     // and if so convert it to a NoClassDefFoundError
   168     // and if so convert it to a NoClassDefFoundError
   163     // And chain the original ClassNotFoundException
   169     // And chain the original ClassNotFoundException
   178       THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), class_name->as_C_string());
   184       THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), class_name->as_C_string());
   179     } else {
   185     } else {
   180       THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), class_name->as_C_string());
   186       THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), class_name->as_C_string());
   181     }
   187     }
   182   }
   188   }
   183   return (klassOop)klass_h();
   189   return (Klass*)klass_h();
   184 }
   190 }
   185 
   191 
   186 
   192 
   187 klassOop SystemDictionary::resolve_or_fail(Symbol* class_name,
   193 Klass* SystemDictionary::resolve_or_fail(Symbol* class_name,
   188                                            bool throw_error, TRAPS)
   194                                            bool throw_error, TRAPS)
   189 {
   195 {
   190   return resolve_or_fail(class_name, Handle(), Handle(), throw_error, THREAD);
   196   return resolve_or_fail(class_name, Handle(), Handle(), throw_error, THREAD);
   191 }
   197 }
   192 
   198 
   193 
   199 
   194 // Forwards to resolve_instance_class_or_null
   200 // Forwards to resolve_instance_class_or_null
   195 
   201 
   196 klassOop SystemDictionary::resolve_or_null(Symbol* class_name, Handle class_loader, Handle protection_domain, TRAPS) {
   202 Klass* SystemDictionary::resolve_or_null(Symbol* class_name, Handle class_loader, Handle protection_domain, TRAPS) {
   197   assert(!THREAD->is_Compiler_thread(),
   203   assert(!THREAD->is_Compiler_thread(),
   198          err_msg("can not load classes with compiler thread: class=%s, classloader=%s",
   204          err_msg("can not load classes with compiler thread: class=%s, classloader=%s",
   199                  class_name->as_C_string(),
   205                  class_name->as_C_string(),
   200                  class_loader.is_null() ? "null" : class_loader->klass()->klass_part()->name()->as_C_string()));
   206                  class_loader.is_null() ? "null" : class_loader->klass()->name()->as_C_string()));
   201   if (FieldType::is_array(class_name)) {
   207   if (FieldType::is_array(class_name)) {
   202     return resolve_array_class_or_null(class_name, class_loader, protection_domain, CHECK_NULL);
   208     return resolve_array_class_or_null(class_name, class_loader, protection_domain, CHECK_NULL);
   203   } else if (FieldType::is_obj(class_name)) {
   209   } else if (FieldType::is_obj(class_name)) {
   204     ResourceMark rm(THREAD);
   210     ResourceMark rm(THREAD);
   205     // Ignore wrapping L and ;.
   211     // Ignore wrapping L and ;.
   209   } else {
   215   } else {
   210     return resolve_instance_class_or_null(class_name, class_loader, protection_domain, CHECK_NULL);
   216     return resolve_instance_class_or_null(class_name, class_loader, protection_domain, CHECK_NULL);
   211   }
   217   }
   212 }
   218 }
   213 
   219 
   214 klassOop SystemDictionary::resolve_or_null(Symbol* class_name, TRAPS) {
   220 Klass* SystemDictionary::resolve_or_null(Symbol* class_name, TRAPS) {
   215   return resolve_or_null(class_name, Handle(), Handle(), THREAD);
   221   return resolve_or_null(class_name, Handle(), Handle(), THREAD);
   216 }
   222 }
   217 
   223 
   218 // Forwards to resolve_instance_class_or_null
   224 // Forwards to resolve_instance_class_or_null
   219 
   225 
   220 klassOop SystemDictionary::resolve_array_class_or_null(Symbol* class_name,
   226 Klass* SystemDictionary::resolve_array_class_or_null(Symbol* class_name,
   221                                                        Handle class_loader,
   227                                                        Handle class_loader,
   222                                                        Handle protection_domain,
   228                                                        Handle protection_domain,
   223                                                        TRAPS) {
   229                                                        TRAPS) {
   224   assert(FieldType::is_array(class_name), "must be array");
   230   assert(FieldType::is_array(class_name), "must be array");
   225   klassOop k = NULL;
   231   Klass* k = NULL;
   226   FieldArrayInfo fd;
   232   FieldArrayInfo fd;
   227   // dimension and object_key in FieldArrayInfo are assigned as a side-effect
   233   // dimension and object_key in FieldArrayInfo are assigned as a side-effect
   228   // of this call
   234   // of this call
   229   BasicType t = FieldType::get_array_info(class_name, fd, CHECK_NULL);
   235   BasicType t = FieldType::get_array_info(class_name, fd, CHECK_NULL);
   230   if (t == T_OBJECT) {
   236   if (t == T_OBJECT) {
   277 //      4.3 calls resolve_super_or_fail Super in parallel on own thread T2
   283 //      4.3 calls resolve_super_or_fail Super in parallel on own thread T2
   278 //      4.4 finds T2, Super -> throws class circularity
   284 //      4.4 finds T2, Super -> throws class circularity
   279 // Must be called, even if superclass is null, since this is
   285 // Must be called, even if superclass is null, since this is
   280 // where the placeholder entry is created which claims this
   286 // where the placeholder entry is created which claims this
   281 // thread is loading this class/classloader.
   287 // thread is loading this class/classloader.
   282 klassOop SystemDictionary::resolve_super_or_fail(Symbol* child_name,
   288 Klass* SystemDictionary::resolve_super_or_fail(Symbol* child_name,
   283                                                  Symbol* class_name,
   289                                                  Symbol* class_name,
   284                                                  Handle class_loader,
   290                                                  Handle class_loader,
   285                                                  Handle protection_domain,
   291                                                  Handle protection_domain,
   286                                                  bool is_superclass,
   292                                                  bool is_superclass,
   287                                                  TRAPS) {
   293                                                  TRAPS) {
   288 
   294 
   289   // Try to get one of the well-known klasses.
   295   // Try to get one of the well-known klasses.
   290   // They are trusted, and do not participate in circularities.
   296   // They are trusted, and do not participate in circularities.
   291   if (LinkWellKnownClasses) {
   297   if (LinkWellKnownClasses) {
   292     klassOop k = find_well_known_klass(class_name);
   298     Klass* k = find_well_known_klass(class_name);
   293     if (k != NULL) {
   299     if (k != NULL) {
   294       return k;
   300       return k;
   295     }
   301     }
   296   }
   302   }
   297 
   303 
   298   // Double-check, if child class is already loaded, just return super-class,interface
   304   // Double-check, if child class is already loaded, just return super-class,interface
   299   // Don't add a placedholder if already loaded, i.e. already in system dictionary
   305   // Don't add a placedholder if already loaded, i.e. already in system dictionary
   300   // Make sure there's a placeholder for the *child* before resolving.
   306   // Make sure there's a placeholder for the *child* before resolving.
   301   // Used as a claim that this thread is currently loading superclass/classloader
   307   // Used as a claim that this thread is currently loading superclass/classloader
   302   // Used here for ClassCircularity checks and also for heap verification
   308   // Used here for ClassCircularity checks and also for heap verification
   303   // (every instanceKlass in the heap needs to be in the system dictionary
   309   // (every InstanceKlass in the heap needs to be in the system dictionary
   304   // or have a placeholder).
   310   // or have a placeholder).
   305   // Must check ClassCircularity before checking if super class is already loaded
   311   // Must check ClassCircularity before checking if super class is already loaded
   306   //
   312   //
   307   // We might not already have a placeholder if this child_name was
   313   // We might not already have a placeholder if this child_name was
   308   // first seen via resolve_from_stream (jni_DefineClass or JVM_DefineClass);
   314   // first seen via resolve_from_stream (jni_DefineClass or JVM_DefineClass);
   309   // the name of the class might not be known until the stream is actually
   315   // the name of the class might not be known until the stream is actually
   310   // parsed.
   316   // parsed.
   311   // Bugs 4643874, 4715493
   317   // Bugs 4643874, 4715493
   312   // compute_hash can have a safepoint
   318   // compute_hash can have a safepoint
   313 
   319 
   314   unsigned int d_hash = dictionary()->compute_hash(child_name, class_loader);
   320   ClassLoaderData* loader_data = class_loader_data(class_loader);
       
   321   unsigned int d_hash = dictionary()->compute_hash(child_name, loader_data);
   315   int d_index = dictionary()->hash_to_index(d_hash);
   322   int d_index = dictionary()->hash_to_index(d_hash);
   316   unsigned int p_hash = placeholders()->compute_hash(child_name, class_loader);
   323   unsigned int p_hash = placeholders()->compute_hash(child_name, loader_data);
   317   int p_index = placeholders()->hash_to_index(p_hash);
   324   int p_index = placeholders()->hash_to_index(p_hash);
   318   // can't throw error holding a lock
   325   // can't throw error holding a lock
   319   bool child_already_loaded = false;
   326   bool child_already_loaded = false;
   320   bool throw_circularity_error = false;
   327   bool throw_circularity_error = false;
   321   {
   328   {
   322     MutexLocker mu(SystemDictionary_lock, THREAD);
   329     MutexLocker mu(SystemDictionary_lock, THREAD);
   323     klassOop childk = find_class(d_index, d_hash, child_name, class_loader);
   330     Klass* childk = find_class(d_index, d_hash, child_name, loader_data);
   324     klassOop quicksuperk;
   331     Klass* quicksuperk;
   325     // to support // loading: if child done loading, just return superclass
   332     // to support // loading: if child done loading, just return superclass
   326     // if class_name, & class_loader don't match:
   333     // if class_name, & class_loader don't match:
   327     // if initial define, SD update will give LinkageError
   334     // if initial define, SD update will give LinkageError
   328     // if redefine: compare_class_versions will give HIERARCHY_CHANGED
   335     // if redefine: compare_class_versions will give HIERARCHY_CHANGED
   329     // so we don't throw an exception here.
   336     // so we don't throw an exception here.
   330     // see: nsk redefclass014 & java.lang.instrument Instrument032
   337     // see: nsk redefclass014 & java.lang.instrument Instrument032
   331     if ((childk != NULL ) && (is_superclass) &&
   338     if ((childk != NULL ) && (is_superclass) &&
   332        ((quicksuperk = instanceKlass::cast(childk)->super()) != NULL) &&
   339        ((quicksuperk = InstanceKlass::cast(childk)->super()) != NULL) &&
   333 
   340 
   334          ((Klass::cast(quicksuperk)->name() == class_name) &&
   341          ((Klass::cast(quicksuperk)->name() == class_name) &&
   335             (Klass::cast(quicksuperk)->class_loader()  == class_loader()))) {
   342             (Klass::cast(quicksuperk)->class_loader()  == class_loader()))) {
   336            return quicksuperk;
   343            return quicksuperk;
   337     } else {
   344     } else {
   338       PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, class_loader);
   345       PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, loader_data);
   339       if (probe && probe->check_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER)) {
   346       if (probe && probe->check_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER)) {
   340           throw_circularity_error = true;
   347           throw_circularity_error = true;
   341       }
   348       }
   342     }
   349     }
   343     if (!throw_circularity_error) {
   350     if (!throw_circularity_error) {
   344       PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, child_name, class_loader, PlaceholderTable::LOAD_SUPER, class_name, THREAD);
   351       PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, child_name, loader_data, PlaceholderTable::LOAD_SUPER, class_name, THREAD);
   345     }
   352     }
   346   }
   353   }
   347   if (throw_circularity_error) {
   354   if (throw_circularity_error) {
   348       ResourceMark rm(THREAD);
   355       ResourceMark rm(THREAD);
   349       THROW_MSG_0(vmSymbols::java_lang_ClassCircularityError(), child_name->as_C_string());
   356       THROW_MSG_0(vmSymbols::java_lang_ClassCircularityError(), child_name->as_C_string());
   350   }
   357   }
   351 
   358 
   352 // java.lang.Object should have been found above
   359 // java.lang.Object should have been found above
   353   assert(class_name != NULL, "null super class for resolving");
   360   assert(class_name != NULL, "null super class for resolving");
   354   // Resolve the super class or interface, check results on return
   361   // Resolve the super class or interface, check results on return
   355   klassOop superk = NULL;
   362   Klass* superk = NULL;
   356   superk = SystemDictionary::resolve_or_null(class_name,
   363   superk = SystemDictionary::resolve_or_null(class_name,
   357                                                  class_loader,
   364                                                  class_loader,
   358                                                  protection_domain,
   365                                                  protection_domain,
   359                                                  THREAD);
   366                                                  THREAD);
   360 
   367 
   363   // Note: clean up of placeholders currently in callers of
   370   // Note: clean up of placeholders currently in callers of
   364   // resolve_super_or_fail - either at update_dictionary time
   371   // resolve_super_or_fail - either at update_dictionary time
   365   // or on error
   372   // or on error
   366   {
   373   {
   367   MutexLocker mu(SystemDictionary_lock, THREAD);
   374   MutexLocker mu(SystemDictionary_lock, THREAD);
   368    PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, class_loader);
   375    PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, loader_data);
   369    if (probe != NULL) {
   376    if (probe != NULL) {
   370       probe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER);
   377       probe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER);
   371    }
   378    }
   372   }
   379   }
   373   if (HAS_PENDING_EXCEPTION || superk_h() == NULL) {
   380   if (HAS_PENDING_EXCEPTION || superk_h() == NULL) {
   391     tty->print_cr("Checking package access");
   398     tty->print_cr("Checking package access");
   392     tty->print(" - class loader:      "); class_loader()->print_value_on(tty);      tty->cr();
   399     tty->print(" - class loader:      "); class_loader()->print_value_on(tty);      tty->cr();
   393     tty->print(" - protection domain: "); protection_domain()->print_value_on(tty); tty->cr();
   400     tty->print(" - protection domain: "); protection_domain()->print_value_on(tty); tty->cr();
   394     tty->print(" - loading:           "); klass()->print_value_on(tty);             tty->cr();
   401     tty->print(" - loading:           "); klass()->print_value_on(tty);             tty->cr();
   395   }
   402   }
   396 
       
   397   assert(class_loader() != NULL, "should not have non-null protection domain for null classloader");
       
   398 
   403 
   399   KlassHandle system_loader(THREAD, SystemDictionary::ClassLoader_klass());
   404   KlassHandle system_loader(THREAD, SystemDictionary::ClassLoader_klass());
   400   JavaCalls::call_special(&result,
   405   JavaCalls::call_special(&result,
   401                          class_loader,
   406                          class_loader,
   402                          system_loader,
   407                          system_loader,
   420   // If no exception has been thrown, we have validated the protection domain
   425   // If no exception has been thrown, we have validated the protection domain
   421   // Insert the protection domain of the initiating class into the set.
   426   // Insert the protection domain of the initiating class into the set.
   422   {
   427   {
   423     // We recalculate the entry here -- we've called out to java since
   428     // We recalculate the entry here -- we've called out to java since
   424     // the last time it was calculated.
   429     // the last time it was calculated.
       
   430     ClassLoaderData* loader_data = class_loader_data(class_loader);
       
   431 
   425     Symbol*  kn = klass->name();
   432     Symbol*  kn = klass->name();
   426     unsigned int d_hash = dictionary()->compute_hash(kn, class_loader);
   433     unsigned int d_hash = dictionary()->compute_hash(kn, loader_data);
   427     int d_index = dictionary()->hash_to_index(d_hash);
   434     int d_index = dictionary()->hash_to_index(d_hash);
   428 
   435 
   429     MutexLocker mu(SystemDictionary_lock, THREAD);
   436     MutexLocker mu(SystemDictionary_lock, THREAD);
   430     {
   437     {
   431       // Note that we have an entry, and entries can be deleted only during GC,
   438       // Note that we have an entry, and entries can be deleted only during GC,
   434       // We're using a No_Safepoint_Verifier to catch any place where we
   441       // We're using a No_Safepoint_Verifier to catch any place where we
   435       // might potentially do a GC at all.
   442       // might potentially do a GC at all.
   436       // SystemDictionary::do_unloading() asserts that classes are only
   443       // SystemDictionary::do_unloading() asserts that classes are only
   437       // unloaded at a safepoint.
   444       // unloaded at a safepoint.
   438       No_Safepoint_Verifier nosafepoint;
   445       No_Safepoint_Verifier nosafepoint;
   439       dictionary()->add_protection_domain(d_index, d_hash, klass, class_loader,
   446       dictionary()->add_protection_domain(d_index, d_hash, klass, loader_data,
   440                                           protection_domain, THREAD);
   447                                           protection_domain, THREAD);
   441     }
   448     }
   442   }
   449   }
   443 }
   450 }
   444 
   451 
   491 // even in non-circularity situations.
   498 // even in non-circularity situations.
   492 // Note: only one thread can define the class, but multiple can resolve
   499 // Note: only one thread can define the class, but multiple can resolve
   493 // Note: must call resolve_super_or_fail even if null super -
   500 // Note: must call resolve_super_or_fail even if null super -
   494 // to force placeholder entry creation for this class for circularity detection
   501 // to force placeholder entry creation for this class for circularity detection
   495 // Caller must check for pending exception
   502 // Caller must check for pending exception
   496 // Returns non-null klassOop if other thread has completed load
   503 // Returns non-null Klass* if other thread has completed load
   497 // and we are done,
   504 // and we are done,
   498 // If return null klassOop and no pending exception, the caller must load the class
   505 // If return null Klass* and no pending exception, the caller must load the class
   499 instanceKlassHandle SystemDictionary::handle_parallel_super_load(
   506 instanceKlassHandle SystemDictionary::handle_parallel_super_load(
   500     Symbol* name, Symbol* superclassname, Handle class_loader,
   507     Symbol* name, Symbol* superclassname, Handle class_loader,
   501     Handle protection_domain, Handle lockObject, TRAPS) {
   508     Handle protection_domain, Handle lockObject, TRAPS) {
   502 
   509 
   503   instanceKlassHandle nh = instanceKlassHandle(); // null Handle
   510   instanceKlassHandle nh = instanceKlassHandle(); // null Handle
   504   unsigned int d_hash = dictionary()->compute_hash(name, class_loader);
   511   ClassLoaderData* loader_data = class_loader_data(class_loader);
       
   512   unsigned int d_hash = dictionary()->compute_hash(name, loader_data);
   505   int d_index = dictionary()->hash_to_index(d_hash);
   513   int d_index = dictionary()->hash_to_index(d_hash);
   506   unsigned int p_hash = placeholders()->compute_hash(name, class_loader);
   514   unsigned int p_hash = placeholders()->compute_hash(name, loader_data);
   507   int p_index = placeholders()->hash_to_index(p_hash);
   515   int p_index = placeholders()->hash_to_index(p_hash);
   508 
   516 
   509   // superk is not used, resolve_super called for circularity check only
   517   // superk is not used, resolve_super called for circularity check only
   510   // This code is reached in two situations. One if this thread
   518   // This code is reached in two situations. One if this thread
   511   // is loading the same class twice (e.g. ClassCircularity, or
   519   // is loading the same class twice (e.g. ClassCircularity, or
   512   // java.lang.instrument).
   520   // java.lang.instrument).
   513   // The second is if another thread started the resolve_super first
   521   // The second is if another thread started the resolve_super first
   514   // and has not yet finished.
   522   // and has not yet finished.
   515   // In both cases the original caller will clean up the placeholder
   523   // In both cases the original caller will clean up the placeholder
   516   // entry on error.
   524   // entry on error.
   517   klassOop superk = SystemDictionary::resolve_super_or_fail(name,
   525   Klass* superk = SystemDictionary::resolve_super_or_fail(name,
   518                                                           superclassname,
   526                                                           superclassname,
   519                                                           class_loader,
   527                                                           class_loader,
   520                                                           protection_domain,
   528                                                           protection_domain,
   521                                                           true,
   529                                                           true,
   522                                                           CHECK_(nh));
   530                                                           CHECK_(nh));
   523   // We don't redefine the class, so we just need to clean up if there
   531   // We don't redefine the class, so we just need to clean up if there
   524   // was not an error (don't want to modify any system dictionary
   532   // was not an error (don't want to modify any system dictionary
   525   // data structures).
   533   // data structures).
   526   {
   534   {
   527     MutexLocker mu(SystemDictionary_lock, THREAD);
   535     MutexLocker mu(SystemDictionary_lock, THREAD);
   528     placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD);
   536     placeholders()->find_and_remove(p_index, p_hash, name, loader_data, THREAD);
   529     SystemDictionary_lock->notify_all();
   537     SystemDictionary_lock->notify_all();
   530   }
   538   }
   531 
   539 
   532   // parallelCapable class loaders do NOT wait for parallel superclass loads to complete
   540   // parallelCapable class loaders do NOT wait for parallel superclass loads to complete
   533   // Serial class loaders and bootstrap classloader do wait for superclass loads
   541   // Serial class loaders and bootstrap classloader do wait for superclass loads
   534  if (!class_loader.is_null() && is_parallelCapable(class_loader)) {
   542  if (!class_loader.is_null() && is_parallelCapable(class_loader)) {
   535     MutexLocker mu(SystemDictionary_lock, THREAD);
   543     MutexLocker mu(SystemDictionary_lock, THREAD);
   536     // Check if classloading completed while we were loading superclass or waiting
   544     // Check if classloading completed while we were loading superclass or waiting
   537     klassOop check = find_class(d_index, d_hash, name, class_loader);
   545     Klass* check = find_class(d_index, d_hash, name, loader_data);
   538     if (check != NULL) {
   546     if (check != NULL) {
   539       // Klass is already loaded, so just return it
   547       // Klass is already loaded, so just return it
   540       return(instanceKlassHandle(THREAD, check));
   548       return(instanceKlassHandle(THREAD, check));
   541     } else {
   549     } else {
   542       return nh;
   550       return nh;
   548   bool super_load_in_progress = true;
   556   bool super_load_in_progress = true;
   549   PlaceholderEntry* placeholder;
   557   PlaceholderEntry* placeholder;
   550   while (super_load_in_progress) {
   558   while (super_load_in_progress) {
   551     MutexLocker mu(SystemDictionary_lock, THREAD);
   559     MutexLocker mu(SystemDictionary_lock, THREAD);
   552     // Check if classloading completed while we were loading superclass or waiting
   560     // Check if classloading completed while we were loading superclass or waiting
   553     klassOop check = find_class(d_index, d_hash, name, class_loader);
   561     Klass* check = find_class(d_index, d_hash, name, loader_data);
   554     if (check != NULL) {
   562     if (check != NULL) {
   555       // Klass is already loaded, so just return it
   563       // Klass is already loaded, so just return it
   556       return(instanceKlassHandle(THREAD, check));
   564       return(instanceKlassHandle(THREAD, check));
   557     } else {
   565     } else {
   558       placeholder = placeholders()->get_entry(p_index, p_hash, name, class_loader);
   566       placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data);
   559       if (placeholder && placeholder->super_load_in_progress() ){
   567       if (placeholder && placeholder->super_load_in_progress() ){
   560         // Before UnsyncloadClass:
   568         // Before UnsyncloadClass:
   561         // We only get here if the application has released the
   569         // We only get here if the application has released the
   562         // classloader lock when another thread was in the middle of loading a
   570         // classloader lock when another thread was in the middle of loading a
   563         // superclass/superinterface for this class, and now
   571         // superclass/superinterface for this class, and now
   566         // load a class should be the one to complete the loading
   574         // load a class should be the one to complete the loading
   567         // with the classfile it initially expected.
   575         // with the classfile it initially expected.
   568         // This logic has the current thread wait once it has done
   576         // This logic has the current thread wait once it has done
   569         // all the superclass/superinterface loading it can, until
   577         // all the superclass/superinterface loading it can, until
   570         // the original thread completes the class loading or fails
   578         // the original thread completes the class loading or fails
   571         // If it completes we will use the resulting instanceKlass
   579         // If it completes we will use the resulting InstanceKlass
   572         // which we will find below in the systemDictionary.
   580         // which we will find below in the systemDictionary.
   573         // We also get here for parallel bootstrap classloader
   581         // We also get here for parallel bootstrap classloader
   574         if (class_loader.is_null()) {
   582         if (class_loader.is_null()) {
   575           SystemDictionary_lock->wait();
   583           SystemDictionary_lock->wait();
   576         } else {
   584         } else {
   584   }
   592   }
   585   return (nh);
   593   return (nh);
   586 }
   594 }
   587 
   595 
   588 
   596 
   589 klassOop SystemDictionary::resolve_instance_class_or_null(Symbol* name, Handle class_loader, Handle protection_domain, TRAPS) {
   597 Klass* SystemDictionary::resolve_instance_class_or_null(Symbol* name, Handle class_loader, Handle protection_domain, TRAPS) {
   590   assert(name != NULL && !FieldType::is_array(name) &&
   598   assert(name != NULL && !FieldType::is_array(name) &&
   591          !FieldType::is_obj(name), "invalid class name");
   599          !FieldType::is_obj(name), "invalid class name");
   592 
   600 
   593   // UseNewReflection
   601   // UseNewReflection
   594   // Fix for 4474172; see evaluation for more details
   602   // Fix for 4474172; see evaluation for more details
   595   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
   603   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
       
   604   ClassLoaderData *loader_data = register_loader(class_loader);
   596 
   605 
   597   // Do lookup to see if class already exist and the protection domain
   606   // Do lookup to see if class already exist and the protection domain
   598   // has the right access
   607   // has the right access
   599   unsigned int d_hash = dictionary()->compute_hash(name, class_loader);
   608   unsigned int d_hash = dictionary()->compute_hash(name, loader_data);
   600   int d_index = dictionary()->hash_to_index(d_hash);
   609   int d_index = dictionary()->hash_to_index(d_hash);
   601   klassOop probe = dictionary()->find(d_index, d_hash, name, class_loader,
   610   Klass* probe = dictionary()->find(d_index, d_hash, name, loader_data,
   602                                       protection_domain, THREAD);
   611                                       protection_domain, THREAD);
   603   if (probe != NULL) return probe;
   612   if (probe != NULL) return probe;
   604 
   613 
   605 
   614 
   606   // Non-bootstrap class loaders will call out to class loader and
   615   // Non-bootstrap class loaders will call out to class loader and
   615   bool DoObjectLock = true;
   624   bool DoObjectLock = true;
   616   if (is_parallelCapable(class_loader)) {
   625   if (is_parallelCapable(class_loader)) {
   617     DoObjectLock = false;
   626     DoObjectLock = false;
   618   }
   627   }
   619 
   628 
   620   unsigned int p_hash = placeholders()->compute_hash(name, class_loader);
   629   unsigned int p_hash = placeholders()->compute_hash(name, loader_data);
   621   int p_index = placeholders()->hash_to_index(p_hash);
   630   int p_index = placeholders()->hash_to_index(p_hash);
   622 
   631 
   623   // Class is not in SystemDictionary so we have to do loading.
   632   // Class is not in SystemDictionary so we have to do loading.
   624   // Make sure we are synchronized on the class loader before we proceed
   633   // Make sure we are synchronized on the class loader before we proceed
   625   Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
   634   Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
   634   PlaceholderEntry* placeholder;
   643   PlaceholderEntry* placeholder;
   635   Symbol* superclassname = NULL;
   644   Symbol* superclassname = NULL;
   636 
   645 
   637   {
   646   {
   638     MutexLocker mu(SystemDictionary_lock, THREAD);
   647     MutexLocker mu(SystemDictionary_lock, THREAD);
   639     klassOop check = find_class(d_index, d_hash, name, class_loader);
   648     Klass* check = find_class(d_index, d_hash, name, loader_data);
   640     if (check != NULL) {
   649     if (check != NULL) {
   641       // Klass is already loaded, so just return it
   650       // Klass is already loaded, so just return it
   642       class_has_been_loaded = true;
   651       class_has_been_loaded = true;
   643       k = instanceKlassHandle(THREAD, check);
   652       k = instanceKlassHandle(THREAD, check);
   644     } else {
   653     } else {
   645       placeholder = placeholders()->get_entry(p_index, p_hash, name, class_loader);
   654       placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data);
   646       if (placeholder && placeholder->super_load_in_progress()) {
   655       if (placeholder && placeholder->super_load_in_progress()) {
   647          super_load_in_progress = true;
   656          super_load_in_progress = true;
   648          if (placeholder->havesupername() == true) {
   657          if (placeholder->havesupername() == true) {
   649            superclassname = placeholder->supername();
   658            superclassname = placeholder->supername();
   650            havesupername = true;
   659            havesupername = true;
   693     //    Allow parallel classloading of a class/classloader pair
   702     //    Allow parallel classloading of a class/classloader pair
   694     bool throw_circularity_error = false;
   703     bool throw_circularity_error = false;
   695     {
   704     {
   696       MutexLocker mu(SystemDictionary_lock, THREAD);
   705       MutexLocker mu(SystemDictionary_lock, THREAD);
   697       if (class_loader.is_null() || !is_parallelCapable(class_loader)) {
   706       if (class_loader.is_null() || !is_parallelCapable(class_loader)) {
   698         PlaceholderEntry* oldprobe = placeholders()->get_entry(p_index, p_hash, name, class_loader);
   707         PlaceholderEntry* oldprobe = placeholders()->get_entry(p_index, p_hash, name, loader_data);
   699         if (oldprobe) {
   708         if (oldprobe) {
   700           // only need check_seen_thread once, not on each loop
   709           // only need check_seen_thread once, not on each loop
   701           // 6341374 java/lang/Instrument with -Xcomp
   710           // 6341374 java/lang/Instrument with -Xcomp
   702           if (oldprobe->check_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE)) {
   711           if (oldprobe->check_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE)) {
   703             throw_circularity_error = true;
   712             throw_circularity_error = true;
   713               // case 2: traditional with broken classloader lock. wait on first
   722               // case 2: traditional with broken classloader lock. wait on first
   714               // requestor.
   723               // requestor.
   715                 double_lock_wait(lockObject, THREAD);
   724                 double_lock_wait(lockObject, THREAD);
   716               }
   725               }
   717               // Check if classloading completed while we were waiting
   726               // Check if classloading completed while we were waiting
   718               klassOop check = find_class(d_index, d_hash, name, class_loader);
   727               Klass* check = find_class(d_index, d_hash, name, loader_data);
   719               if (check != NULL) {
   728               if (check != NULL) {
   720                 // Klass is already loaded, so just return it
   729                 // Klass is already loaded, so just return it
   721                 k = instanceKlassHandle(THREAD, check);
   730                 k = instanceKlassHandle(THREAD, check);
   722                 class_has_been_loaded = true;
   731                 class_has_been_loaded = true;
   723               }
   732               }
   724               // check if other thread failed to load and cleaned up
   733               // check if other thread failed to load and cleaned up
   725               oldprobe = placeholders()->get_entry(p_index, p_hash, name, class_loader);
   734               oldprobe = placeholders()->get_entry(p_index, p_hash, name, loader_data);
   726             }
   735             }
   727           }
   736           }
   728         }
   737         }
   729       }
   738       }
   730       // All cases: add LOAD_INSTANCE
   739       // All cases: add LOAD_INSTANCE
   731       // case 3: UnsyncloadClass || case 5: parallelCapable: allow competing threads to try
   740       // case 3: UnsyncloadClass || case 5: parallelCapable: allow competing threads to try
   732       // LOAD_INSTANCE in parallel
   741       // LOAD_INSTANCE in parallel
   733       // add placeholder entry even if error - callers will remove on error
   742       // add placeholder entry even if error - callers will remove on error
   734       if (!throw_circularity_error && !class_has_been_loaded) {
   743       if (!throw_circularity_error && !class_has_been_loaded) {
   735         PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, name, class_loader, PlaceholderTable::LOAD_INSTANCE, NULL, THREAD);
   744         PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, NULL, THREAD);
   736         // For class loaders that do not acquire the classloader object lock,
   745         // For class loaders that do not acquire the classloader object lock,
   737         // if they did not catch another thread holding LOAD_INSTANCE,
   746         // if they did not catch another thread holding LOAD_INSTANCE,
   738         // need a check analogous to the acquire ObjectLocker/find_class
   747         // need a check analogous to the acquire ObjectLocker/find_class
   739         // i.e. now that we hold the LOAD_INSTANCE token on loading this class/CL
   748         // i.e. now that we hold the LOAD_INSTANCE token on loading this class/CL
   740         // one final check if the load has already completed
   749         // one final check if the load has already completed
   741         // class loaders holding the ObjectLock shouldn't find the class here
   750         // class loaders holding the ObjectLock shouldn't find the class here
   742         klassOop check = find_class(d_index, d_hash, name, class_loader);
   751         Klass* check = find_class(d_index, d_hash, name, loader_data);
   743         if (check != NULL) {
   752         if (check != NULL) {
   744         // Klass is already loaded, so just return it
   753         // Klass is already loaded, so just return it
   745           k = instanceKlassHandle(THREAD, check);
   754           k = instanceKlassHandle(THREAD, check);
   746           class_has_been_loaded = true;
   755           class_has_been_loaded = true;
   747           newprobe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE);
   756           newprobe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE);
   748           placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD);
   757           placeholders()->find_and_remove(p_index, p_hash, name, loader_data, THREAD);
   749           SystemDictionary_lock->notify_all();
   758           SystemDictionary_lock->notify_all();
   750         }
   759         }
   751       }
   760       }
   752     }
   761     }
   753     // must throw error outside of owning lock
   762     // must throw error outside of owning lock
   763 
   772 
   764       // For UnsyncloadClass only
   773       // For UnsyncloadClass only
   765       // If they got a linkageError, check if a parallel class load succeeded.
   774       // If they got a linkageError, check if a parallel class load succeeded.
   766       // If it did, then for bytecode resolution the specification requires
   775       // If it did, then for bytecode resolution the specification requires
   767       // that we return the same result we did for the other thread, i.e. the
   776       // that we return the same result we did for the other thread, i.e. the
   768       // successfully loaded instanceKlass
   777       // successfully loaded InstanceKlass
   769       // Should not get here for classloaders that support parallelism
   778       // Should not get here for classloaders that support parallelism
   770       // with the new cleaner mechanism, even with AllowParallelDefineClass
   779       // with the new cleaner mechanism, even with AllowParallelDefineClass
   771       // Bootstrap goes through here to allow for an extra guarantee check
   780       // Bootstrap goes through here to allow for an extra guarantee check
   772       if (UnsyncloadClass || (class_loader.is_null())) {
   781       if (UnsyncloadClass || (class_loader.is_null())) {
   773         if (k.is_null() && HAS_PENDING_EXCEPTION
   782         if (k.is_null() && HAS_PENDING_EXCEPTION
   774           && PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
   783           && PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
   775           MutexLocker mu(SystemDictionary_lock, THREAD);
   784           MutexLocker mu(SystemDictionary_lock, THREAD);
   776           klassOop check = find_class(d_index, d_hash, name, class_loader);
   785           Klass* check = find_class(d_index, d_hash, name, loader_data);
   777           if (check != NULL) {
   786           if (check != NULL) {
   778             // Klass is already loaded, so just use it
   787             // Klass is already loaded, so just use it
   779             k = instanceKlassHandle(THREAD, check);
   788             k = instanceKlassHandle(THREAD, check);
   780             CLEAR_PENDING_EXCEPTION;
   789             CLEAR_PENDING_EXCEPTION;
   781             guarantee((!class_loader.is_null()), "dup definition for bootstrap loader?");
   790             guarantee((!class_loader.is_null()), "dup definition for bootstrap loader?");
   787       // This cleans up LOAD_INSTANCE entries
   796       // This cleans up LOAD_INSTANCE entries
   788       // It also cleans up LOAD_SUPER entries on errors from
   797       // It also cleans up LOAD_SUPER entries on errors from
   789       // calling load_instance_class
   798       // calling load_instance_class
   790       {
   799       {
   791         MutexLocker mu(SystemDictionary_lock, THREAD);
   800         MutexLocker mu(SystemDictionary_lock, THREAD);
   792         PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, name, class_loader);
   801         PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, name, loader_data);
   793         if (probe != NULL) {
   802         if (probe != NULL) {
   794           probe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE);
   803           probe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE);
   795           placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD);
   804           placeholders()->find_and_remove(p_index, p_hash, name, loader_data, THREAD);
   796           SystemDictionary_lock->notify_all();
   805           SystemDictionary_lock->notify_all();
   797         }
   806         }
   798       }
   807       }
   799 
   808 
   800       // If everything was OK (no exceptions, no null return value), and
   809       // If everything was OK (no exceptions, no null return value), and
   822       }
   831       }
   823       if (HAS_PENDING_EXCEPTION || k.is_null()) {
   832       if (HAS_PENDING_EXCEPTION || k.is_null()) {
   824         // On error, clean up placeholders
   833         // On error, clean up placeholders
   825         {
   834         {
   826           MutexLocker mu(SystemDictionary_lock, THREAD);
   835           MutexLocker mu(SystemDictionary_lock, THREAD);
   827           placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD);
   836           placeholders()->find_and_remove(p_index, p_hash, name, loader_data, THREAD);
   828           SystemDictionary_lock->notify_all();
   837           SystemDictionary_lock->notify_all();
   829         }
   838         }
   830         return NULL;
   839         return NULL;
   831       }
   840       }
   832     }
   841     }
   833   }
   842   }
   834 
   843 
   835 #ifdef ASSERT
   844 #ifdef ASSERT
   836   {
   845   {
   837     Handle loader (THREAD, k->class_loader());
   846     ClassLoaderData* loader_data = k->class_loader_data();
   838     MutexLocker mu(SystemDictionary_lock, THREAD);
   847     MutexLocker mu(SystemDictionary_lock, THREAD);
   839     oop kk = find_class(name, loader);
   848     Klass* kk = find_class(name, loader_data);
   840     assert(kk == k(), "should be present in dictionary");
   849     assert(kk == k(), "should be present in dictionary");
   841   }
   850   }
   842 #endif
   851 #endif
   843 
   852 
   844   // return if the protection domain in NULL
   853   // return if the protection domain in NULL
   853     // might potentially do a GC at all.
   862     // might potentially do a GC at all.
   854     // SystemDictionary::do_unloading() asserts that classes are only
   863     // SystemDictionary::do_unloading() asserts that classes are only
   855     // unloaded at a safepoint.
   864     // unloaded at a safepoint.
   856     No_Safepoint_Verifier nosafepoint;
   865     No_Safepoint_Verifier nosafepoint;
   857     if (dictionary()->is_valid_protection_domain(d_index, d_hash, name,
   866     if (dictionary()->is_valid_protection_domain(d_index, d_hash, name,
   858                                                  class_loader,
   867                                                  loader_data,
   859                                                  protection_domain)) {
   868                                                  protection_domain)) {
   860       return k();
   869       return k();
   861     }
   870     }
   862   }
   871   }
   863 
   872 
   864   // Verify protection domain. If it fails an exception is thrown
   873   // Verify protection domain. If it fails an exception is thrown
   865   validate_protection_domain(k, class_loader, protection_domain, CHECK_(klassOop(NULL)));
   874   validate_protection_domain(k, class_loader, protection_domain, CHECK_NULL);
   866 
   875 
   867   return k();
   876   return k();
   868 }
   877 }
   869 
   878 
   870 
   879 
   877 //
   886 //
   878 // Callers should be aware that an entry could be added just after
   887 // Callers should be aware that an entry could be added just after
   879 // _dictionary->bucket(index) is read here, so the caller will not see
   888 // _dictionary->bucket(index) is read here, so the caller will not see
   880 // the new entry.
   889 // the new entry.
   881 
   890 
   882 klassOop SystemDictionary::find(Symbol* class_name,
   891 Klass* SystemDictionary::find(Symbol* class_name,
   883                                 Handle class_loader,
   892                                 Handle class_loader,
   884                                 Handle protection_domain,
   893                                 Handle protection_domain,
   885                                 TRAPS) {
   894                                 TRAPS) {
   886 
   895 
   887   // UseNewReflection
   896   // UseNewReflection
   888   // The result of this call should be consistent with the result
   897   // The result of this call should be consistent with the result
   889   // of the call to resolve_instance_class_or_null().
   898   // of the call to resolve_instance_class_or_null().
   890   // See evaluation 6790209 and 4474172 for more details.
   899   // See evaluation 6790209 and 4474172 for more details.
   891   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
   900   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
   892 
   901   ClassLoaderData* loader_data = register_loader(class_loader);
   893   unsigned int d_hash = dictionary()->compute_hash(class_name, class_loader);
   902 
       
   903   unsigned int d_hash = dictionary()->compute_hash(class_name, loader_data);
   894   int d_index = dictionary()->hash_to_index(d_hash);
   904   int d_index = dictionary()->hash_to_index(d_hash);
   895 
   905 
   896   {
   906   {
   897     // Note that we have an entry, and entries can be deleted only during GC,
   907     // Note that we have an entry, and entries can be deleted only during GC,
   898     // so we cannot allow GC to occur while we're holding this entry.
   908     // so we cannot allow GC to occur while we're holding this entry.
   899     // We're using a No_Safepoint_Verifier to catch any place where we
   909     // We're using a No_Safepoint_Verifier to catch any place where we
   900     // might potentially do a GC at all.
   910     // might potentially do a GC at all.
   901     // SystemDictionary::do_unloading() asserts that classes are only
   911     // SystemDictionary::do_unloading() asserts that classes are only
   902     // unloaded at a safepoint.
   912     // unloaded at a safepoint.
   903     No_Safepoint_Verifier nosafepoint;
   913     No_Safepoint_Verifier nosafepoint;
   904     return dictionary()->find(d_index, d_hash, class_name, class_loader,
   914     return dictionary()->find(d_index, d_hash, class_name, loader_data,
   905                               protection_domain, THREAD);
   915                               protection_domain, THREAD);
   906   }
   916   }
   907 }
   917 }
   908 
   918 
   909 
   919 
   910 // Look for a loaded instance or array klass by name.  Do not do any loading.
   920 // Look for a loaded instance or array klass by name.  Do not do any loading.
   911 // return NULL in case of error.
   921 // return NULL in case of error.
   912 klassOop SystemDictionary::find_instance_or_array_klass(Symbol* class_name,
   922 Klass* SystemDictionary::find_instance_or_array_klass(Symbol* class_name,
   913                                                         Handle class_loader,
   923                                                         Handle class_loader,
   914                                                         Handle protection_domain,
   924                                                         Handle protection_domain,
   915                                                         TRAPS) {
   925                                                         TRAPS) {
   916   klassOop k = NULL;
   926   Klass* k = NULL;
   917   assert(class_name != NULL, "class name must be non NULL");
   927   assert(class_name != NULL, "class name must be non NULL");
   918 
   928 
   919   // Try to get one of the well-known klasses.
   929   // Try to get one of the well-known klasses.
   920   if (LinkWellKnownClasses) {
   930   if (LinkWellKnownClasses) {
   921     k = find_well_known_klass(class_name);
   931     k = find_well_known_klass(class_name);
   951 static int find_wkk_calls, find_wkk_probes, find_wkk_wins;
   961 static int find_wkk_calls, find_wkk_probes, find_wkk_wins;
   952 // counts for "hello world": 3983, 1616, 1075
   962 // counts for "hello world": 3983, 1616, 1075
   953 //  => 60% hit after limit guard, 25% total win rate
   963 //  => 60% hit after limit guard, 25% total win rate
   954 #endif
   964 #endif
   955 
   965 
   956 klassOop SystemDictionary::find_well_known_klass(Symbol* class_name) {
   966 Klass* SystemDictionary::find_well_known_klass(Symbol* class_name) {
   957   // A bounds-check on class_name will quickly get a negative result.
   967   // A bounds-check on class_name will quickly get a negative result.
   958   NOT_PRODUCT(find_wkk_calls++);
   968   NOT_PRODUCT(find_wkk_calls++);
   959   if (class_name >= wk_klass_name_limits[0] &&
   969   if (class_name >= wk_klass_name_limits[0] &&
   960       class_name <= wk_klass_name_limits[1]) {
   970       class_name <= wk_klass_name_limits[1]) {
   961     NOT_PRODUCT(find_wkk_probes++);
   971     NOT_PRODUCT(find_wkk_probes++);
   962     vmSymbols::SID sid = vmSymbols::find_sid(class_name);
   972     vmSymbols::SID sid = vmSymbols::find_sid(class_name);
   963     if (sid != vmSymbols::NO_SID) {
   973     if (sid != vmSymbols::NO_SID) {
   964       klassOop k = NULL;
   974       Klass* k = NULL;
   965       switch (sid) {
   975       switch (sid) {
   966         #define WK_KLASS_CASE(name, symbol, ignore_option) \
   976         #define WK_KLASS_CASE(name, symbol, ignore_option) \
   967         case vmSymbols::VM_SYMBOL_ENUM_NAME(symbol): \
   977         case vmSymbols::VM_SYMBOL_ENUM_NAME(symbol): \
   968           k = WK_KLASS(name); break;
   978           k = WK_KLASS(name); break;
   969         WK_KLASSES_DO(WK_KLASS_CASE)
   979         WK_KLASSES_DO(WK_KLASS_CASE)
   977 }
   987 }
   978 
   988 
   979 // Note: this method is much like resolve_from_stream, but
   989 // Note: this method is much like resolve_from_stream, but
   980 // updates no supplemental data structures.
   990 // updates no supplemental data structures.
   981 // TODO consolidate the two methods with a helper routine?
   991 // TODO consolidate the two methods with a helper routine?
   982 klassOop SystemDictionary::parse_stream(Symbol* class_name,
   992 Klass* SystemDictionary::parse_stream(Symbol* class_name,
   983                                         Handle class_loader,
   993                                         Handle class_loader,
   984                                         Handle protection_domain,
   994                                         Handle protection_domain,
   985                                         ClassFileStream* st,
   995                                         ClassFileStream* st,
   986                                         KlassHandle host_klass,
   996                                         KlassHandle host_klass,
   987                                         GrowableArray<Handle>* cp_patches,
   997                                         GrowableArray<Handle>* cp_patches,
  1011   // was an error or not (don't want to modify any system dictionary
  1021   // was an error or not (don't want to modify any system dictionary
  1012   // data structures).
  1022   // data structures).
  1013   // Parsed name could be null if we threw an error before we got far
  1023   // Parsed name could be null if we threw an error before we got far
  1014   // enough along to parse it -- in that case, there is nothing to clean up.
  1024   // enough along to parse it -- in that case, there is nothing to clean up.
  1015   if (parsed_name != NULL) {
  1025   if (parsed_name != NULL) {
       
  1026     ClassLoaderData* loader_data = class_loader_data(class_loader);
       
  1027 
  1016     unsigned int p_hash = placeholders()->compute_hash(parsed_name,
  1028     unsigned int p_hash = placeholders()->compute_hash(parsed_name,
  1017                                                        class_loader);
  1029                                                        loader_data);
  1018     int p_index = placeholders()->hash_to_index(p_hash);
  1030     int p_index = placeholders()->hash_to_index(p_hash);
  1019     {
  1031     {
  1020     MutexLocker mu(SystemDictionary_lock, THREAD);
  1032     MutexLocker mu(SystemDictionary_lock, THREAD);
  1021     placeholders()->find_and_remove(p_index, p_hash, parsed_name, class_loader, THREAD);
  1033     placeholders()->find_and_remove(p_index, p_hash, parsed_name, loader_data, THREAD);
  1022     SystemDictionary_lock->notify_all();
  1034     SystemDictionary_lock->notify_all();
  1023     }
  1035     }
  1024   }
  1036   }
  1025 
  1037 
  1026   if (host_klass.not_null() && k.not_null()) {
  1038   if (host_klass.not_null() && k.not_null()) {
  1027     assert(EnableInvokeDynamic, "");
  1039     assert(EnableInvokeDynamic, "");
  1028     // If it's anonymous, initialize it now, since nobody else will.
  1040     // If it's anonymous, initialize it now, since nobody else will.
       
  1041     k->class_loader_data()->record_dependency(host_klass(), CHECK_NULL);
  1029     k->set_host_klass(host_klass());
  1042     k->set_host_klass(host_klass());
  1030 
  1043 
  1031     {
  1044     {
  1032       MutexLocker mu_r(Compile_lock, THREAD);
  1045       MutexLocker mu_r(Compile_lock, THREAD);
  1033 
  1046 
  1036       add_to_hierarchy(k, CHECK_NULL); // No exception, but can block
  1049       add_to_hierarchy(k, CHECK_NULL); // No exception, but can block
  1037 
  1050 
  1038       // But, do not add to system dictionary.
  1051       // But, do not add to system dictionary.
  1039     }
  1052     }
  1040 
  1053 
       
  1054     // Rewrite and patch constant pool here.
       
  1055     k->link_class(THREAD);
       
  1056     if (cp_patches != NULL) {
       
  1057       k->constants()->patch_resolved_references(cp_patches);
       
  1058     }
  1041     k->eager_initialize(THREAD);
  1059     k->eager_initialize(THREAD);
  1042 
  1060 
  1043     // notify jvmti
  1061     // notify jvmti
  1044     if (JvmtiExport::should_post_class_load()) {
  1062     if (JvmtiExport::should_post_class_load()) {
  1045         assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
  1063         assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
  1046         JvmtiExport::post_class_load((JavaThread *) THREAD, k());
  1064         JvmtiExport::post_class_load((JavaThread *) THREAD, k());
  1047     }
  1065     }
  1048   }
  1066   }
       
  1067   assert(host_klass.not_null() || cp_patches == NULL,
       
  1068          "cp_patches only found with host_klass");
  1049 
  1069 
  1050   return k();
  1070   return k();
  1051 }
  1071 }
  1052 
  1072 
  1053 // Add a klass to the system from a stream (called by jni_DefineClass and
  1073 // Add a klass to the system from a stream (called by jni_DefineClass and
  1054 // JVM_DefineClass).
  1074 // JVM_DefineClass).
  1055 // Note: class_name can be NULL. In that case we do not know the name of
  1075 // Note: class_name can be NULL. In that case we do not know the name of
  1056 // the class until we have parsed the stream.
  1076 // the class until we have parsed the stream.
  1057 
  1077 
  1058 klassOop SystemDictionary::resolve_from_stream(Symbol* class_name,
  1078 Klass* SystemDictionary::resolve_from_stream(Symbol* class_name,
  1059                                                Handle class_loader,
  1079                                                Handle class_loader,
  1060                                                Handle protection_domain,
  1080                                                Handle protection_domain,
  1061                                                ClassFileStream* st,
  1081                                                ClassFileStream* st,
  1062                                                bool verify,
  1082                                                bool verify,
  1063                                                TRAPS) {
  1083                                                TRAPS) {
  1066   // or all classloaders with UnsyncloadClass do not acquire lock here
  1086   // or all classloaders with UnsyncloadClass do not acquire lock here
  1067   bool DoObjectLock = true;
  1087   bool DoObjectLock = true;
  1068   if (is_parallelCapable(class_loader)) {
  1088   if (is_parallelCapable(class_loader)) {
  1069     DoObjectLock = false;
  1089     DoObjectLock = false;
  1070   }
  1090   }
       
  1091 
       
  1092   ClassLoaderData* loader_data = register_loader(class_loader);
  1071 
  1093 
  1072   // Make sure we are synchronized on the class loader before we proceed
  1094   // Make sure we are synchronized on the class loader before we proceed
  1073   Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
  1095   Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
  1074   check_loader_lock_contention(lockObject, THREAD);
  1096   check_loader_lock_contention(lockObject, THREAD);
  1075   ObjectLocker ol(lockObject, THREAD, DoObjectLock);
  1097   ObjectLocker ol(lockObject, THREAD, DoObjectLock);
  1123     assert(is_internal_format(parsed_name),
  1145     assert(is_internal_format(parsed_name),
  1124            "external class name format used internally");
  1146            "external class name format used internally");
  1125 
  1147 
  1126     // Add class just loaded
  1148     // Add class just loaded
  1127     // If a class loader supports parallel classloading handle parallel define requests
  1149     // If a class loader supports parallel classloading handle parallel define requests
  1128     // find_or_define_instance_class may return a different instanceKlass
  1150     // find_or_define_instance_class may return a different InstanceKlass
  1129     if (is_parallelCapable(class_loader)) {
  1151     if (is_parallelCapable(class_loader)) {
  1130       k = find_or_define_instance_class(class_name, class_loader, k, THREAD);
  1152       k = find_or_define_instance_class(class_name, class_loader, k, THREAD);
  1131     } else {
  1153     } else {
  1132       define_instance_class(k, THREAD);
  1154       define_instance_class(k, THREAD);
  1133     }
  1155     }
  1138   // must make sure parsed_name is valid first (it won't be if we had
  1160   // must make sure parsed_name is valid first (it won't be if we had
  1139   // a format error before the class was parsed far enough to
  1161   // a format error before the class was parsed far enough to
  1140   // find the name).
  1162   // find the name).
  1141   if (HAS_PENDING_EXCEPTION && parsed_name != NULL) {
  1163   if (HAS_PENDING_EXCEPTION && parsed_name != NULL) {
  1142     unsigned int p_hash = placeholders()->compute_hash(parsed_name,
  1164     unsigned int p_hash = placeholders()->compute_hash(parsed_name,
  1143                                                        class_loader);
  1165                                                        loader_data);
  1144     int p_index = placeholders()->hash_to_index(p_hash);
  1166     int p_index = placeholders()->hash_to_index(p_hash);
  1145     {
  1167     {
  1146     MutexLocker mu(SystemDictionary_lock, THREAD);
  1168     MutexLocker mu(SystemDictionary_lock, THREAD);
  1147     placeholders()->find_and_remove(p_index, p_hash, parsed_name, class_loader, THREAD);
  1169     placeholders()->find_and_remove(p_index, p_hash, parsed_name, loader_data, THREAD);
  1148     SystemDictionary_lock->notify_all();
  1170     SystemDictionary_lock->notify_all();
  1149     }
  1171     }
  1150     return NULL;
  1172     return NULL;
  1151   }
  1173   }
  1152 
  1174 
  1154   // SystemDictionary; this is only done on success
  1176   // SystemDictionary; this is only done on success
  1155   debug_only( {
  1177   debug_only( {
  1156     if (!HAS_PENDING_EXCEPTION) {
  1178     if (!HAS_PENDING_EXCEPTION) {
  1157       assert(parsed_name != NULL, "parsed_name is still null?");
  1179       assert(parsed_name != NULL, "parsed_name is still null?");
  1158       Symbol*  h_name    = k->name();
  1180       Symbol*  h_name    = k->name();
  1159       Handle h_loader (THREAD, k->class_loader());
  1181       ClassLoaderData *defining_loader_data = k->class_loader_data();
  1160 
  1182 
  1161       MutexLocker mu(SystemDictionary_lock, THREAD);
  1183       MutexLocker mu(SystemDictionary_lock, THREAD);
  1162 
  1184 
  1163       klassOop check = find_class(parsed_name, class_loader);
  1185       Klass* check = find_class(parsed_name, loader_data);
  1164       assert(check == k(), "should be present in the dictionary");
  1186       assert(check == k(), "should be present in the dictionary");
  1165 
  1187 
  1166       klassOop check2 = find_class(h_name, h_loader);
  1188       Klass* check2 = find_class(h_name, defining_loader_data);
  1167       assert(check == check2, "name inconsistancy in SystemDictionary");
  1189       assert(check == check2, "name inconsistancy in SystemDictionary");
  1168     }
  1190     }
  1169   } );
  1191   } );
  1170 
  1192 
  1171   return k();
  1193   return k();
  1181 
  1203 
  1182 
  1204 
  1183 // If there is a shared dictionary, then find the entry for the
  1205 // If there is a shared dictionary, then find the entry for the
  1184 // given shared system class, if any.
  1206 // given shared system class, if any.
  1185 
  1207 
  1186 klassOop SystemDictionary::find_shared_class(Symbol* class_name) {
  1208 Klass* SystemDictionary::find_shared_class(Symbol* class_name) {
  1187   if (shared_dictionary() != NULL) {
  1209   if (shared_dictionary() != NULL) {
  1188     unsigned int d_hash = shared_dictionary()->compute_hash(class_name, Handle());
  1210     unsigned int d_hash = shared_dictionary()->compute_hash(class_name, NULL);
  1189     int d_index = shared_dictionary()->hash_to_index(d_hash);
  1211     int d_index = shared_dictionary()->hash_to_index(d_hash);
       
  1212 
  1190     return shared_dictionary()->find_shared_class(d_index, d_hash, class_name);
  1213     return shared_dictionary()->find_shared_class(d_index, d_hash, class_name);
  1191   } else {
  1214   } else {
  1192     return NULL;
  1215     return NULL;
  1193   }
  1216   }
  1194 }
  1217 }
  1221     // Found the class, now load the superclass and interfaces.  If they
  1244     // Found the class, now load the superclass and interfaces.  If they
  1222     // are shared, add them to the main system dictionary and reset
  1245     // are shared, add them to the main system dictionary and reset
  1223     // their hierarchy references (supers, subs, and interfaces).
  1246     // their hierarchy references (supers, subs, and interfaces).
  1224 
  1247 
  1225     if (ik->super() != NULL) {
  1248     if (ik->super() != NULL) {
  1226       Symbol*  cn = ik->super()->klass_part()->name();
  1249       Symbol*  cn = ik->super()->name();
  1227       resolve_super_or_fail(class_name, cn,
  1250       resolve_super_or_fail(class_name, cn,
  1228                             class_loader, Handle(), true, CHECK_(nh));
  1251                             class_loader, Handle(), true, CHECK_(nh));
  1229     }
  1252     }
  1230 
  1253 
  1231     objArrayHandle interfaces (THREAD, ik->local_interfaces());
  1254     Array<Klass*>* interfaces = ik->local_interfaces();
  1232     int num_interfaces = interfaces->length();
  1255     int num_interfaces = interfaces->length();
  1233     for (int index = 0; index < num_interfaces; index++) {
  1256     for (int index = 0; index < num_interfaces; index++) {
  1234       klassOop k = klassOop(interfaces->obj_at(index));
  1257       Klass* k = interfaces->at(index);
  1235 
  1258 
  1236       // Note: can not use instanceKlass::cast here because
  1259       // Note: can not use InstanceKlass::cast here because
  1237       // interfaces' instanceKlass's C++ vtbls haven't been
  1260       // interfaces' InstanceKlass's C++ vtbls haven't been
  1238       // reinitialized yet (they will be once the interface classes
  1261       // reinitialized yet (they will be once the interface classes
  1239       // are loaded)
  1262       // are loaded)
  1240       Symbol*  name  = k->klass_part()->name();
  1263       Symbol*  name  = k->name();
  1241       resolve_super_or_fail(class_name, name, class_loader, Handle(), false, CHECK_(nh));
  1264       resolve_super_or_fail(class_name, name, class_loader, Handle(), false, CHECK_(nh));
  1242     }
  1265     }
  1243 
  1266 
  1244     // Adjust methods to recover missing data.  They need addresses for
  1267     // Adjust methods to recover missing data.  They need addresses for
  1245     // interpreter entry points and their default native method address
  1268     // interpreter entry points and their default native method address
  1253 
  1276 
  1254     {
  1277     {
  1255       Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
  1278       Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
  1256       check_loader_lock_contention(lockObject, THREAD);
  1279       check_loader_lock_contention(lockObject, THREAD);
  1257       ObjectLocker ol(lockObject, THREAD, true);
  1280       ObjectLocker ol(lockObject, THREAD, true);
  1258 
  1281       ik->restore_unshareable_info(CHECK_(nh));
  1259       objArrayHandle methods (THREAD, ik->methods());
       
  1260       int num_methods = methods->length();
       
  1261       for (int index2 = 0; index2 < num_methods; ++index2) {
       
  1262         methodHandle m(THREAD, methodOop(methods->obj_at(index2)));
       
  1263         m()->link_method(m, CHECK_(nh));
       
  1264       }
       
  1265       if (JvmtiExport::has_redefined_a_class()) {
       
  1266         // Reinitialize vtable because RedefineClasses may have changed some
       
  1267         // entries in this vtable for super classes so the CDS vtable might
       
  1268         // point to old or obsolete entries.  RedefineClasses doesn't fix up
       
  1269         // vtables in the shared system dictionary, only the main one.
       
  1270         // It also redefines the itable too so fix that too.
       
  1271         ResourceMark rm(THREAD);
       
  1272         ik->vtable()->initialize_vtable(false, CHECK_(nh));
       
  1273         ik->itable()->initialize_itable(false, CHECK_(nh));
       
  1274       }
       
  1275     }
  1282     }
  1276 
  1283 
  1277     if (TraceClassLoading) {
  1284     if (TraceClassLoading) {
  1278       ResourceMark rm;
  1285       ResourceMark rm;
  1279       tty->print("[Loaded %s", ik->external_name());
  1286       tty->print("[Loaded %s", ik->external_name());
  1280       tty->print(" from shared objects file");
  1287       tty->print(" from shared objects file");
  1281       tty->print_cr("]");
  1288       tty->print_cr("]");
  1282     }
  1289     }
  1283     // notify a class loaded from shared object
  1290     // notify a class loaded from shared object
  1284     ClassLoadingService::notify_class_loaded(instanceKlass::cast(ik()),
  1291     ClassLoadingService::notify_class_loaded(InstanceKlass::cast(ik()),
  1285                                              true /* shared class */);
  1292                                              true /* shared class */);
  1286   }
  1293   }
  1287   return ik;
  1294   return ik;
  1288 }
  1295 }
  1289 
  1296 
  1295 // first try to call this so it'd better be fast!!
  1302 // first try to call this so it'd better be fast!!
  1296 static instanceKlassHandle download_and_retry_class_load(
  1303 static instanceKlassHandle download_and_retry_class_load(
  1297                                                     Symbol* class_name,
  1304                                                     Symbol* class_name,
  1298                                                     TRAPS) {
  1305                                                     TRAPS) {
  1299 
  1306 
  1300   klassOop dlm = SystemDictionary::DownloadManager_klass();
  1307   Klass* dlm = SystemDictionary::DownloadManager_klass();
  1301   instanceKlassHandle nk;
  1308   instanceKlassHandle nk;
  1302 
  1309 
  1303   // If download manager class isn't loaded just return.
  1310   // If download manager class isn't loaded just return.
  1304   if (dlm == NULL) return nk;
  1311   if (dlm == NULL) return nk;
  1305 
  1312 
  1373     if (k.is_null()) {
  1380     if (k.is_null()) {
  1374       k = download_and_retry_class_load(class_name, CHECK_(nh));
  1381       k = download_and_retry_class_load(class_name, CHECK_(nh));
  1375     }
  1382     }
  1376 #endif // KERNEL
  1383 #endif // KERNEL
  1377 
  1384 
  1378     // find_or_define_instance_class may return a different instanceKlass
  1385     // find_or_define_instance_class may return a different InstanceKlass
  1379     if (!k.is_null()) {
  1386     if (!k.is_null()) {
  1380       k = find_or_define_instance_class(class_name, class_loader, k, CHECK_(nh));
  1387       k = find_or_define_instance_class(class_name, class_loader, k, CHECK_(nh));
  1381     }
  1388     }
  1382     return k;
  1389     return k;
  1383   } else {
  1390   } else {
  1442 
  1449 
  1443     // Primitive classes return null since forName() can not be
  1450     // Primitive classes return null since forName() can not be
  1444     // used to obtain any of the Class objects representing primitives or void
  1451     // used to obtain any of the Class objects representing primitives or void
  1445     if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
  1452     if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
  1446       instanceKlassHandle k =
  1453       instanceKlassHandle k =
  1447                 instanceKlassHandle(THREAD, java_lang_Class::as_klassOop(obj));
  1454                 instanceKlassHandle(THREAD, java_lang_Class::as_Klass(obj));
  1448       // For user defined Java class loaders, check that the name returned is
  1455       // For user defined Java class loaders, check that the name returned is
  1449       // the same as that requested.  This check is done for the bootstrap
  1456       // the same as that requested.  This check is done for the bootstrap
  1450       // loader when parsing the class file.
  1457       // loader when parsing the class file.
  1451       if (class_name == k->name()) {
  1458       if (class_name == k->name()) {
  1452         return k;
  1459         return k;
  1457   }
  1464   }
  1458 }
  1465 }
  1459 
  1466 
  1460 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) {
  1467 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) {
  1461 
  1468 
  1462   Handle class_loader_h(THREAD, k->class_loader());
  1469   ClassLoaderData* loader_data = k->class_loader_data();
       
  1470   Handle class_loader_h(THREAD, loader_data->class_loader());
       
  1471 
       
  1472   for (uintx it = 0; it < GCExpandToAllocateDelayMillis; it++){}
  1463 
  1473 
  1464  // for bootstrap and other parallel classloaders don't acquire lock,
  1474  // for bootstrap and other parallel classloaders don't acquire lock,
  1465  // use placeholder token
  1475  // use placeholder token
  1466  // If a parallelCapable class loader calls define_instance_class instead of
  1476  // If a parallelCapable class loader calls define_instance_class instead of
  1467  // find_or_define_instance_class to get here, we have a timing
  1477  // find_or_define_instance_class to get here, we have a timing
  1480   // Existing classloaders will call define_instance_class with the
  1490   // Existing classloaders will call define_instance_class with the
  1481   // classloader lock held
  1491   // classloader lock held
  1482   // Parallel classloaders will call find_or_define_instance_class
  1492   // Parallel classloaders will call find_or_define_instance_class
  1483   // which will require a token to perform the define class
  1493   // which will require a token to perform the define class
  1484   Symbol*  name_h = k->name();
  1494   Symbol*  name_h = k->name();
  1485   unsigned int d_hash = dictionary()->compute_hash(name_h, class_loader_h);
  1495   unsigned int d_hash = dictionary()->compute_hash(name_h, loader_data);
  1486   int d_index = dictionary()->hash_to_index(d_hash);
  1496   int d_index = dictionary()->hash_to_index(d_hash);
  1487   check_constraints(d_index, d_hash, k, class_loader_h, true, CHECK);
  1497   check_constraints(d_index, d_hash, k, class_loader_h, true, CHECK);
  1488 
  1498 
  1489   // Register class just loaded with class loader (placed in Vector)
       
  1490   // Note we do this before updating the dictionary, as this can
       
  1491   // fail with an OutOfMemoryError (if it does, we will *not* put this
       
  1492   // class in the dictionary and will not update the class hierarchy).
       
  1493   if (k->class_loader() != NULL) {
       
  1494     methodHandle m(THREAD, Universe::loader_addClass_method());
       
  1495     JavaValue result(T_VOID);
       
  1496     JavaCallArguments args(class_loader_h);
       
  1497     args.push_oop(Handle(THREAD, k->java_mirror()));
       
  1498     JavaCalls::call(&result, m, &args, CHECK);
       
  1499   }
       
  1500 
       
  1501   // Add the new class. We need recompile lock during update of CHA.
  1499   // Add the new class. We need recompile lock during update of CHA.
  1502   {
  1500   {
  1503     unsigned int p_hash = placeholders()->compute_hash(name_h, class_loader_h);
  1501     unsigned int p_hash = placeholders()->compute_hash(name_h, loader_data);
  1504     int p_index = placeholders()->hash_to_index(p_hash);
  1502     int p_index = placeholders()->hash_to_index(p_hash);
  1505 
  1503 
  1506     MutexLocker mu_r(Compile_lock, THREAD);
  1504     MutexLocker mu_r(Compile_lock, THREAD);
  1507 
  1505 
  1508     // Add to class hierarchy, initialize vtables, and do possible
  1506     // Add to class hierarchy, initialize vtables, and do possible
  1542 // Note: VM callers should ensure consistency of k/class_name,class_loader
  1540 // Note: VM callers should ensure consistency of k/class_name,class_loader
  1543 instanceKlassHandle SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader, instanceKlassHandle k, TRAPS) {
  1541 instanceKlassHandle SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader, instanceKlassHandle k, TRAPS) {
  1544 
  1542 
  1545   instanceKlassHandle nh = instanceKlassHandle(); // null Handle
  1543   instanceKlassHandle nh = instanceKlassHandle(); // null Handle
  1546   Symbol*  name_h = k->name(); // passed in class_name may be null
  1544   Symbol*  name_h = k->name(); // passed in class_name may be null
  1547 
  1545   ClassLoaderData* loader_data = class_loader_data(class_loader);
  1548   unsigned int d_hash = dictionary()->compute_hash(name_h, class_loader);
  1546 
       
  1547   unsigned int d_hash = dictionary()->compute_hash(name_h, loader_data);
  1549   int d_index = dictionary()->hash_to_index(d_hash);
  1548   int d_index = dictionary()->hash_to_index(d_hash);
  1550 
  1549 
  1551 // Hold SD lock around find_class and placeholder creation for DEFINE_CLASS
  1550 // Hold SD lock around find_class and placeholder creation for DEFINE_CLASS
  1552   unsigned int p_hash = placeholders()->compute_hash(name_h, class_loader);
  1551   unsigned int p_hash = placeholders()->compute_hash(name_h, loader_data);
  1553   int p_index = placeholders()->hash_to_index(p_hash);
  1552   int p_index = placeholders()->hash_to_index(p_hash);
  1554   PlaceholderEntry* probe;
  1553   PlaceholderEntry* probe;
  1555 
  1554 
  1556   {
  1555   {
  1557     MutexLocker mu(SystemDictionary_lock, THREAD);
  1556     MutexLocker mu(SystemDictionary_lock, THREAD);
  1558     // First check if class already defined
  1557     // First check if class already defined
  1559     if (UnsyncloadClass || (is_parallelDefine(class_loader))) {
  1558     if (UnsyncloadClass || (is_parallelDefine(class_loader))) {
  1560       klassOop check = find_class(d_index, d_hash, name_h, class_loader);
  1559       Klass* check = find_class(d_index, d_hash, name_h, loader_data);
  1561       if (check != NULL) {
  1560       if (check != NULL) {
  1562         return(instanceKlassHandle(THREAD, check));
  1561         return(instanceKlassHandle(THREAD, check));
  1563       }
  1562       }
  1564     }
  1563     }
  1565 
  1564 
  1566     // Acquire define token for this class/classloader
  1565     // Acquire define token for this class/classloader
  1567     probe = placeholders()->find_and_add(p_index, p_hash, name_h, class_loader, PlaceholderTable::DEFINE_CLASS, NULL, THREAD);
  1566     probe = placeholders()->find_and_add(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, NULL, THREAD);
  1568     // Wait if another thread defining in parallel
  1567     // Wait if another thread defining in parallel
  1569     // All threads wait - even those that will throw duplicate class: otherwise
  1568     // All threads wait - even those that will throw duplicate class: otherwise
  1570     // caller is surprised by LinkageError: duplicate, but findLoadedClass fails
  1569     // caller is surprised by LinkageError: duplicate, but findLoadedClass fails
  1571     // if other thread has not finished updating dictionary
  1570     // if other thread has not finished updating dictionary
  1572     while (probe->definer() != NULL) {
  1571     while (probe->definer() != NULL) {
  1573       SystemDictionary_lock->wait();
  1572       SystemDictionary_lock->wait();
  1574     }
  1573     }
  1575     // Only special cases allow parallel defines and can use other thread's results
  1574     // Only special cases allow parallel defines and can use other thread's results
  1576     // Other cases fall through, and may run into duplicate defines
  1575     // Other cases fall through, and may run into duplicate defines
  1577     // caught by finding an entry in the SystemDictionary
  1576     // caught by finding an entry in the SystemDictionary
  1578     if ((UnsyncloadClass || is_parallelDefine(class_loader)) && (probe->instanceKlass() != NULL)) {
  1577     if ((UnsyncloadClass || is_parallelDefine(class_loader)) && (probe->InstanceKlass() != NULL)) {
  1579         probe->remove_seen_thread(THREAD, PlaceholderTable::DEFINE_CLASS);
  1578         probe->remove_seen_thread(THREAD, PlaceholderTable::DEFINE_CLASS);
  1580         placeholders()->find_and_remove(p_index, p_hash, name_h, class_loader, THREAD);
  1579         placeholders()->find_and_remove(p_index, p_hash, name_h, loader_data, THREAD);
  1581         SystemDictionary_lock->notify_all();
  1580         SystemDictionary_lock->notify_all();
  1582 #ifdef ASSERT
  1581 #ifdef ASSERT
  1583         klassOop check = find_class(d_index, d_hash, name_h, class_loader);
  1582         Klass* check = find_class(d_index, d_hash, name_h, loader_data);
  1584         assert(check != NULL, "definer missed recording success");
  1583         assert(check != NULL, "definer missed recording success");
  1585 #endif
  1584 #endif
  1586         return(instanceKlassHandle(THREAD, probe->instanceKlass()));
  1585         return(instanceKlassHandle(THREAD, probe->InstanceKlass()));
  1587     } else {
  1586     } else {
  1588       // This thread will define the class (even if earlier thread tried and had an error)
  1587       // This thread will define the class (even if earlier thread tried and had an error)
  1589       probe->set_definer(THREAD);
  1588       probe->set_definer(THREAD);
  1590     }
  1589     }
  1591   }
  1590   }
  1595   Handle linkage_exception = Handle(); // null handle
  1594   Handle linkage_exception = Handle(); // null handle
  1596 
  1595 
  1597   // definer must notify any waiting threads
  1596   // definer must notify any waiting threads
  1598   {
  1597   {
  1599     MutexLocker mu(SystemDictionary_lock, THREAD);
  1598     MutexLocker mu(SystemDictionary_lock, THREAD);
  1600     PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, name_h, class_loader);
  1599     PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, name_h, loader_data);
  1601     assert(probe != NULL, "DEFINE_CLASS placeholder lost?");
  1600     assert(probe != NULL, "DEFINE_CLASS placeholder lost?");
  1602     if (probe != NULL) {
  1601     if (probe != NULL) {
  1603       if (HAS_PENDING_EXCEPTION) {
  1602       if (HAS_PENDING_EXCEPTION) {
  1604         linkage_exception = Handle(THREAD,PENDING_EXCEPTION);
  1603         linkage_exception = Handle(THREAD,PENDING_EXCEPTION);
  1605         CLEAR_PENDING_EXCEPTION;
  1604         CLEAR_PENDING_EXCEPTION;
  1606       } else {
  1605       } else {
  1607         probe->set_instanceKlass(k());
  1606         probe->set_instanceKlass(k());
  1608       }
  1607       }
  1609       probe->set_definer(NULL);
  1608       probe->set_definer(NULL);
  1610       probe->remove_seen_thread(THREAD, PlaceholderTable::DEFINE_CLASS);
  1609       probe->remove_seen_thread(THREAD, PlaceholderTable::DEFINE_CLASS);
  1611       placeholders()->find_and_remove(p_index, p_hash, name_h, class_loader, THREAD);
  1610       placeholders()->find_and_remove(p_index, p_hash, name_h, loader_data, THREAD);
  1612       SystemDictionary_lock->notify_all();
  1611       SystemDictionary_lock->notify_all();
  1613     }
  1612     }
  1614   }
  1613   }
  1615 
  1614 
  1616   // Can't throw exception while holding lock due to rank ordering
  1615   // Can't throw exception while holding lock due to rank ordering
  1653 }
  1652 }
  1654 
  1653 
  1655 // ----------------------------------------------------------------------------
  1654 // ----------------------------------------------------------------------------
  1656 // Lookup
  1655 // Lookup
  1657 
  1656 
  1658 klassOop SystemDictionary::find_class(int index, unsigned int hash,
  1657 Klass* SystemDictionary::find_class(int index, unsigned int hash,
  1659                                       Symbol* class_name,
  1658                                       Symbol* class_name,
  1660                                       Handle class_loader) {
  1659                                       ClassLoaderData* loader_data) {
  1661   assert_locked_or_safepoint(SystemDictionary_lock);
  1660   assert_locked_or_safepoint(SystemDictionary_lock);
  1662   assert (index == dictionary()->index_for(class_name, class_loader),
  1661   assert (index == dictionary()->index_for(class_name, loader_data),
  1663           "incorrect index?");
  1662           "incorrect index?");
  1664 
  1663 
  1665   klassOop k = dictionary()->find_class(index, hash, class_name, class_loader);
  1664   Klass* k = dictionary()->find_class(index, hash, class_name, loader_data);
  1666   return k;
  1665   return k;
  1667 }
  1666 }
  1668 
  1667 
  1669 
  1668 
  1670 // Basic find on classes in the midst of being loaded
  1669 // Basic find on classes in the midst of being loaded
  1671 Symbol* SystemDictionary::find_placeholder(Symbol* class_name,
  1670 Symbol* SystemDictionary::find_placeholder(Symbol* class_name,
  1672                                            Handle class_loader) {
  1671                                            ClassLoaderData* loader_data) {
  1673   assert_locked_or_safepoint(SystemDictionary_lock);
  1672   assert_locked_or_safepoint(SystemDictionary_lock);
  1674   unsigned int p_hash = placeholders()->compute_hash(class_name, class_loader);
  1673   unsigned int p_hash = placeholders()->compute_hash(class_name, loader_data);
  1675   int p_index = placeholders()->hash_to_index(p_hash);
  1674   int p_index = placeholders()->hash_to_index(p_hash);
  1676   return placeholders()->find_entry(p_index, p_hash, class_name, class_loader);
  1675   return placeholders()->find_entry(p_index, p_hash, class_name, loader_data);
  1677 }
  1676 }
  1678 
  1677 
  1679 
  1678 
  1680 // Used for assertions and verification only
  1679 // Used for assertions and verification only
  1681 klassOop SystemDictionary::find_class(Symbol* class_name, Handle class_loader) {
  1680 Klass* SystemDictionary::find_class(Symbol* class_name, ClassLoaderData* loader_data) {
  1682   #ifndef ASSERT
  1681   #ifndef ASSERT
  1683   guarantee(VerifyBeforeGC   ||
  1682   guarantee(VerifyBeforeGC   ||
  1684             VerifyDuringGC   ||
  1683             VerifyDuringGC   ||
  1685             VerifyBeforeExit ||
  1684             VerifyBeforeExit ||
  1686             VerifyAfterGC, "too expensive");
  1685             VerifyAfterGC, "too expensive");
  1687   #endif
  1686   #endif
  1688   assert_locked_or_safepoint(SystemDictionary_lock);
  1687   assert_locked_or_safepoint(SystemDictionary_lock);
  1689 
  1688 
  1690   // First look in the loaded class array
  1689   // First look in the loaded class array
  1691   unsigned int d_hash = dictionary()->compute_hash(class_name, class_loader);
  1690   unsigned int d_hash = dictionary()->compute_hash(class_name, loader_data);
  1692   int d_index = dictionary()->hash_to_index(d_hash);
  1691   int d_index = dictionary()->hash_to_index(d_hash);
  1693   return find_class(d_index, d_hash, class_name, class_loader);
  1692   return find_class(d_index, d_hash, class_name, loader_data);
  1694 }
  1693 }
  1695 
  1694 
  1696 
  1695 
  1697 // Get the next class in the diictionary.
  1696 // Get the next class in the diictionary.
  1698 klassOop SystemDictionary::try_get_next_class() {
  1697 Klass* SystemDictionary::try_get_next_class() {
  1699   return dictionary()->try_get_next_class();
  1698   return dictionary()->try_get_next_class();
  1700 }
  1699 }
  1701 
  1700 
  1702 
  1701 
  1703 // ----------------------------------------------------------------------------
  1702 // ----------------------------------------------------------------------------
  1710   assert_locked_or_safepoint(Compile_lock);
  1709   assert_locked_or_safepoint(Compile_lock);
  1711 
  1710 
  1712   // Link into hierachy. Make sure the vtables are initialized before linking into
  1711   // Link into hierachy. Make sure the vtables are initialized before linking into
  1713   k->append_to_sibling_list();                    // add to superklass/sibling list
  1712   k->append_to_sibling_list();                    // add to superklass/sibling list
  1714   k->process_interfaces(THREAD);                  // handle all "implements" declarations
  1713   k->process_interfaces(THREAD);                  // handle all "implements" declarations
  1715   k->set_init_state(instanceKlass::loaded);
  1714   k->set_init_state(InstanceKlass::loaded);
  1716   // Now flush all code that depended on old class hierarchy.
  1715   // Now flush all code that depended on old class hierarchy.
  1717   // Note: must be done *after* linking k into the hierarchy (was bug 12/9/97)
  1716   // Note: must be done *after* linking k into the hierarchy (was bug 12/9/97)
  1718   // Also, first reinitialize vtable because it may have gotten out of synch
  1717   // Also, first reinitialize vtable because it may have gotten out of synch
  1719   // while the new class wasn't connected to the class hierarchy.
  1718   // while the new class wasn't connected to the class hierarchy.
  1720   Universe::flush_dependents_on(k);
  1719   Universe::flush_dependents_on(k);
  1731 //
  1730 //
  1732 // The second phase removes (unloads) unreachable classes from the
  1731 // The second phase removes (unloads) unreachable classes from the
  1733 // system dictionary and follows the remaining classes' contents.
  1732 // system dictionary and follows the remaining classes' contents.
  1734 
  1733 
  1735 void SystemDictionary::always_strong_oops_do(OopClosure* blk) {
  1734 void SystemDictionary::always_strong_oops_do(OopClosure* blk) {
  1736   // Follow preloaded classes/mirrors and system loader object
       
  1737   blk->do_oop(&_java_system_loader);
  1735   blk->do_oop(&_java_system_loader);
  1738   preloaded_oops_do(blk);
  1736   blk->do_oop(&_system_loader_lock_obj);
  1739   always_strong_classes_do(blk);
  1737 
  1740 }
  1738   dictionary()->always_strong_oops_do(blk);
  1741 
  1739 }
  1742 
  1740 
  1743 void SystemDictionary::always_strong_classes_do(OopClosure* blk) {
  1741 void SystemDictionary::always_strong_classes_do(KlassClosure* closure) {
  1744   // Follow all system classes and temporary placeholders in dictionary
  1742   // Follow all system classes and temporary placeholders in dictionary
  1745   dictionary()->always_strong_classes_do(blk);
  1743   dictionary()->always_strong_classes_do(closure);
  1746 
  1744 
  1747   // Placeholders. These are *always* strong roots, as they
  1745   // Placeholders. These represent classes we're actively loading.
  1748   // represent classes we're actively loading.
  1746   placeholders()->classes_do(closure);
  1749   placeholders_do(blk);
       
  1750 
       
  1751   // Visit extra methods
       
  1752   invoke_method_table()->oops_do(blk);
       
  1753 }
       
  1754 
       
  1755 
       
  1756 void SystemDictionary::placeholders_do(OopClosure* blk) {
       
  1757   placeholders()->oops_do(blk);
       
  1758 }
  1747 }
  1759 
  1748 
  1760 // Calculate a "good" systemdictionary size based
  1749 // Calculate a "good" systemdictionary size based
  1761 // on predicted or current loaded classes count
  1750 // on predicted or current loaded classes count
  1762 int SystemDictionary::calculate_systemdictionary_size(int classcount) {
  1751 int SystemDictionary::calculate_systemdictionary_size(int classcount) {
  1771     }
  1760     }
  1772   }
  1761   }
  1773   return newsize;
  1762   return newsize;
  1774 }
  1763 }
  1775 bool SystemDictionary::do_unloading(BoolObjectClosure* is_alive) {
  1764 bool SystemDictionary::do_unloading(BoolObjectClosure* is_alive) {
  1776   bool result = dictionary()->do_unloading(is_alive);
  1765   // First, mark for unload all ClassLoaderData referencing a dead class loader.
  1777   constraints()->purge_loader_constraints(is_alive);
  1766   bool has_dead_loaders = ClassLoaderDataGraph::do_unloading(is_alive);
  1778   resolution_errors()->purge_resolution_errors(is_alive);
  1767   bool unloading_occurred = false;
  1779   return result;
  1768   if (has_dead_loaders) {
  1780 }
  1769     unloading_occurred = dictionary()->do_unloading();
  1781 
  1770     constraints()->purge_loader_constraints();
  1782 
  1771     resolution_errors()->purge_resolution_errors();
  1783 // The mirrors are scanned by shared_oops_do() which is
  1772 }
  1784 // not called by oops_do().  In order to process oops in
  1773   return unloading_occurred;
  1785 // a necessary order, shared_oops_do() is call by
  1774 }
  1786 // Universe::oops_do().
  1775 
  1787 void SystemDictionary::oops_do(OopClosure* f) {
  1776 void SystemDictionary::oops_do(OopClosure* f) {
  1788   // Adjust preloaded classes and system loader object
       
  1789   f->do_oop(&_java_system_loader);
  1777   f->do_oop(&_java_system_loader);
  1790   preloaded_oops_do(f);
  1778   f->do_oop(&_system_loader_lock_obj);
  1791 
       
  1792   lazily_loaded_oops_do(f);
       
  1793 
  1779 
  1794   // Adjust dictionary
  1780   // Adjust dictionary
  1795   dictionary()->oops_do(f);
  1781   dictionary()->oops_do(f);
  1796 
  1782 
  1797   // Visit extra methods
  1783   // Visit extra methods
  1798   invoke_method_table()->oops_do(f);
  1784   invoke_method_table()->oops_do(f);
  1799 
  1785 }
  1800   // Partially loaded classes
  1786 
  1801   placeholders()->oops_do(f);
  1787 // Extended Class redefinition support.
  1802 
  1788 // If one of these classes is replaced, we need to replace it in these places.
  1803   // Adjust constraint table
  1789 // KlassClosure::do_klass should take the address of a class but we can
  1804   constraints()->oops_do(f);
  1790 // change that later.
  1805 
  1791 void SystemDictionary::preloaded_classes_do(KlassClosure* f) {
  1806   // Adjust resolution error table
       
  1807   resolution_errors()->oops_do(f);
       
  1808 }
       
  1809 
       
  1810 
       
  1811 void SystemDictionary::preloaded_oops_do(OopClosure* f) {
       
  1812   for (int k = (int)FIRST_WKID; k < (int)WKID_LIMIT; k++) {
  1792   for (int k = (int)FIRST_WKID; k < (int)WKID_LIMIT; k++) {
  1813     f->do_oop((oop*) &_well_known_klasses[k]);
  1793     f->do_klass(_well_known_klasses[k]);
  1814   }
  1794   }
  1815 
  1795 
  1816   {
  1796   {
  1817     for (int i = 0; i < T_VOID+1; i++) {
  1797     for (int i = 0; i < T_VOID+1; i++) {
  1818       if (_box_klasses[i] != NULL) {
  1798       if (_box_klasses[i] != NULL) {
  1819         assert(i >= T_BOOLEAN, "checking");
  1799         assert(i >= T_BOOLEAN, "checking");
  1820         f->do_oop((oop*) &_box_klasses[i]);
  1800         f->do_klass(_box_klasses[i]);
  1821       }
  1801       }
  1822     }
  1802     }
  1823   }
  1803   }
  1824 
  1804 
  1825   // The basic type mirrors would have already been processed in
  1805   FilteredFieldsMap::classes_do(f);
  1826   // Universe::oops_do(), via a call to shared_oops_do(), so should
  1806 }
  1827   // not be processed again.
  1807 
  1828 
  1808 void SystemDictionary::lazily_loaded_classes_do(KlassClosure* f) {
  1829   f->do_oop((oop*) &_system_loader_lock_obj);
  1809   f->do_klass(_abstract_ownable_synchronizer_klass);
  1830   FilteredFieldsMap::klasses_oops_do(f);
       
  1831 }
       
  1832 
       
  1833 void SystemDictionary::lazily_loaded_oops_do(OopClosure* f) {
       
  1834   f->do_oop((oop*) &_abstract_ownable_synchronizer_klass);
       
  1835 }
  1810 }
  1836 
  1811 
  1837 // Just the classes from defining class loaders
  1812 // Just the classes from defining class loaders
  1838 // Don't iterate over placeholders
  1813 // Don't iterate over placeholders
  1839 void SystemDictionary::classes_do(void f(klassOop)) {
  1814 void SystemDictionary::classes_do(void f(Klass*)) {
  1840   dictionary()->classes_do(f);
  1815   dictionary()->classes_do(f);
  1841 }
  1816 }
  1842 
  1817 
  1843 // Added for initialize_itable_for_klass
  1818 // Added for initialize_itable_for_klass
  1844 //   Just the classes from defining class loaders
  1819 //   Just the classes from defining class loaders
  1845 // Don't iterate over placeholders
  1820 // Don't iterate over placeholders
  1846 void SystemDictionary::classes_do(void f(klassOop, TRAPS), TRAPS) {
  1821 void SystemDictionary::classes_do(void f(Klass*, TRAPS), TRAPS) {
  1847   dictionary()->classes_do(f, CHECK);
  1822   dictionary()->classes_do(f, CHECK);
  1848 }
  1823 }
  1849 
  1824 
  1850 //   All classes, and their class loaders
  1825 //   All classes, and their class loaders
  1851 // Don't iterate over placeholders
  1826 // Don't iterate over placeholders
  1852 void SystemDictionary::classes_do(void f(klassOop, oop)) {
  1827 void SystemDictionary::classes_do(void f(Klass*, ClassLoaderData*)) {
  1853   dictionary()->classes_do(f);
  1828   dictionary()->classes_do(f);
  1854 }
  1829 }
  1855 
  1830 
  1856 //   All classes, and their class loaders
  1831 //   All classes, and their class loaders
  1857 //   (added for helpers that use HandleMarks and ResourceMarks)
  1832 //   (added for helpers that use HandleMarks and ResourceMarks)
  1858 // Don't iterate over placeholders
  1833 // Don't iterate over placeholders
  1859 void SystemDictionary::classes_do(void f(klassOop, oop, TRAPS), TRAPS) {
  1834 void SystemDictionary::classes_do(void f(Klass*, ClassLoaderData*, TRAPS), TRAPS) {
  1860   dictionary()->classes_do(f, CHECK);
  1835   dictionary()->classes_do(f, CHECK);
  1861 }
  1836 }
  1862 
  1837 
  1863 void SystemDictionary::placeholders_do(void f(Symbol*, oop)) {
  1838 void SystemDictionary::placeholders_do(void f(Symbol*)) {
  1864   placeholders()->entries_do(f);
  1839   placeholders()->entries_do(f);
  1865 }
  1840 }
  1866 
  1841 
  1867 void SystemDictionary::methods_do(void f(methodOop)) {
  1842 void SystemDictionary::methods_do(void f(Method*)) {
  1868   dictionary()->methods_do(f);
  1843   dictionary()->methods_do(f);
  1869   invoke_method_table()->methods_do(f);
  1844   invoke_method_table()->methods_do(f);
  1870 }
  1845 }
  1871 
  1846 
  1872 // ----------------------------------------------------------------------------
  1847 // ----------------------------------------------------------------------------
  1876   assert(JDK_Version::is_gte_jdk16x_version(), "Must be JDK 1.6 or later");
  1851   assert(JDK_Version::is_gte_jdk16x_version(), "Must be JDK 1.6 or later");
  1877 
  1852 
  1878   // if multiple threads calling this function, only one thread will load
  1853   // if multiple threads calling this function, only one thread will load
  1879   // the class.  The other threads will find the loaded version once the
  1854   // the class.  The other threads will find the loaded version once the
  1880   // class is loaded.
  1855   // class is loaded.
  1881   klassOop aos = _abstract_ownable_synchronizer_klass;
  1856   Klass* aos = _abstract_ownable_synchronizer_klass;
  1882   if (aos == NULL) {
  1857   if (aos == NULL) {
  1883     klassOop k = resolve_or_fail(vmSymbols::java_util_concurrent_locks_AbstractOwnableSynchronizer(), true, CHECK);
  1858     Klass* k = resolve_or_fail(vmSymbols::java_util_concurrent_locks_AbstractOwnableSynchronizer(), true, CHECK);
  1884     // Force a fence to prevent any read before the write completes
  1859     // Force a fence to prevent any read before the write completes
  1885     OrderAccess::fence();
  1860     OrderAccess::fence();
  1886     _abstract_ownable_synchronizer_klass = k;
  1861     _abstract_ownable_synchronizer_klass = k;
  1887   }
  1862   }
  1888 }
  1863 }
  1901   _loader_constraints  = new LoaderConstraintTable(_loader_constraint_size);
  1876   _loader_constraints  = new LoaderConstraintTable(_loader_constraint_size);
  1902   _resolution_errors   = new ResolutionErrorTable(_resolution_error_size);
  1877   _resolution_errors   = new ResolutionErrorTable(_resolution_error_size);
  1903   _invoke_method_table = new SymbolPropertyTable(_invoke_method_size);
  1878   _invoke_method_table = new SymbolPropertyTable(_invoke_method_size);
  1904 
  1879 
  1905   // Allocate private object used as system class loader lock
  1880   // Allocate private object used as system class loader lock
  1906   _system_loader_lock_obj = oopFactory::new_system_objArray(0, CHECK);
  1881   _system_loader_lock_obj = oopFactory::new_intArray(0, CHECK);
  1907   // Initialize basic classes
  1882   // Initialize basic classes
  1908   initialize_preloaded_classes(CHECK);
  1883   initialize_preloaded_classes(CHECK);
  1909 }
  1884 }
  1910 
  1885 
  1911 // Compact table of directions on the initialization of klasses:
  1886 // Compact table of directions on the initialization of klasses:
  1922 bool SystemDictionary::initialize_wk_klass(WKID id, int init_opt, TRAPS) {
  1897 bool SystemDictionary::initialize_wk_klass(WKID id, int init_opt, TRAPS) {
  1923   assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
  1898   assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
  1924   int  info = wk_init_info[id - FIRST_WKID];
  1899   int  info = wk_init_info[id - FIRST_WKID];
  1925   int  sid  = (info >> CEIL_LG_OPTION_LIMIT);
  1900   int  sid  = (info >> CEIL_LG_OPTION_LIMIT);
  1926   Symbol* symbol = vmSymbols::symbol_at((vmSymbols::SID)sid);
  1901   Symbol* symbol = vmSymbols::symbol_at((vmSymbols::SID)sid);
  1927   klassOop*    klassp = &_well_known_klasses[id];
  1902   Klass**    klassp = &_well_known_klasses[id];
  1928   bool must_load = (init_opt < SystemDictionary::Opt);
  1903   bool must_load = (init_opt < SystemDictionary::Opt);
  1929   bool try_load  = true;
  1904   bool try_load  = true;
  1930   if (init_opt == SystemDictionary::Opt_Kernel) {
  1905   if (init_opt == SystemDictionary::Opt_Kernel) {
  1931 #ifndef KERNEL
  1906 #ifndef KERNEL
  1932     try_load = false;
  1907     try_load = false;
  1970 
  1945 
  1971 void SystemDictionary::initialize_preloaded_classes(TRAPS) {
  1946 void SystemDictionary::initialize_preloaded_classes(TRAPS) {
  1972   assert(WK_KLASS(Object_klass) == NULL, "preloaded classes should only be initialized once");
  1947   assert(WK_KLASS(Object_klass) == NULL, "preloaded classes should only be initialized once");
  1973   // Preload commonly used klasses
  1948   // Preload commonly used klasses
  1974   WKID scan = FIRST_WKID;
  1949   WKID scan = FIRST_WKID;
  1975   // first do Object, String, Class
  1950   // first do Object, then String, Class
       
  1951   if (UseSharedSpaces) {
       
  1952     initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Object_klass), scan, CHECK);
       
  1953     // Initialize the constant pool for the Object_class
       
  1954     InstanceKlass* ik = InstanceKlass::cast(Object_klass());
       
  1955     ik->constants()->restore_unshareable_info(CHECK);
  1976   initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Class_klass), scan, CHECK);
  1956   initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Class_klass), scan, CHECK);
       
  1957   } else {
       
  1958     initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Class_klass), scan, CHECK);
       
  1959   }
  1977 
  1960 
  1978   // Calculate offsets for String and Class classes since they are loaded and
  1961   // Calculate offsets for String and Class classes since they are loaded and
  1979   // can be used after this point.
  1962   // can be used after this point.
  1980   java_lang_String::compute_offsets();
  1963   java_lang_String::compute_offsets();
  1981   java_lang_Class::compute_offsets();
  1964   java_lang_Class::compute_offsets();
  1990 
  1973 
  1991   // do a bunch more:
  1974   // do a bunch more:
  1992   initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Reference_klass), scan, CHECK);
  1975   initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(Reference_klass), scan, CHECK);
  1993 
  1976 
  1994   // Preload ref klasses and set reference types
  1977   // Preload ref klasses and set reference types
  1995   instanceKlass::cast(WK_KLASS(Reference_klass))->set_reference_type(REF_OTHER);
  1978   InstanceKlass::cast(WK_KLASS(Reference_klass))->set_reference_type(REF_OTHER);
  1996   instanceRefKlass::update_nonstatic_oop_maps(WK_KLASS(Reference_klass));
  1979   instanceRefKlass::update_nonstatic_oop_maps(WK_KLASS(Reference_klass));
  1997 
  1980 
  1998   initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(PhantomReference_klass), scan, CHECK);
  1981   initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(PhantomReference_klass), scan, CHECK);
  1999   instanceKlass::cast(WK_KLASS(SoftReference_klass))->set_reference_type(REF_SOFT);
  1982   InstanceKlass::cast(WK_KLASS(SoftReference_klass))->set_reference_type(REF_SOFT);
  2000   instanceKlass::cast(WK_KLASS(WeakReference_klass))->set_reference_type(REF_WEAK);
  1983   InstanceKlass::cast(WK_KLASS(WeakReference_klass))->set_reference_type(REF_WEAK);
  2001   instanceKlass::cast(WK_KLASS(FinalReference_klass))->set_reference_type(REF_FINAL);
  1984   InstanceKlass::cast(WK_KLASS(FinalReference_klass))->set_reference_type(REF_FINAL);
  2002   instanceKlass::cast(WK_KLASS(PhantomReference_klass))->set_reference_type(REF_PHANTOM);
  1985   InstanceKlass::cast(WK_KLASS(PhantomReference_klass))->set_reference_type(REF_PHANTOM);
  2003 
  1986 
  2004   // JSR 292 classes
  1987   // JSR 292 classes
  2005   WKID jsr292_group_start = WK_KLASS_ENUM_NAME(MethodHandle_klass);
  1988   WKID jsr292_group_start = WK_KLASS_ENUM_NAME(MethodHandle_klass);
  2006   WKID jsr292_group_end   = WK_KLASS_ENUM_NAME(VolatileCallSite_klass);
  1989   WKID jsr292_group_end   = WK_KLASS_ENUM_NAME(VolatileCallSite_klass);
  2007   initialize_wk_klasses_until(jsr292_group_start, scan, CHECK);
  1990   initialize_wk_klasses_until(jsr292_group_start, scan, CHECK);
  2030     warning("Cannot find sun/jkernel/DownloadManager");
  2013     warning("Cannot find sun/jkernel/DownloadManager");
  2031   }
  2014   }
  2032 #endif // KERNEL
  2015 #endif // KERNEL
  2033 
  2016 
  2034   { // Compute whether we should use loadClass or loadClassInternal when loading classes.
  2017   { // Compute whether we should use loadClass or loadClassInternal when loading classes.
  2035     methodOop method = instanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::loadClassInternal_name(), vmSymbols::string_class_signature());
  2018     Method* method = InstanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::loadClassInternal_name(), vmSymbols::string_class_signature());
  2036     _has_loadClassInternal = (method != NULL);
  2019     _has_loadClassInternal = (method != NULL);
  2037   }
  2020   }
  2038   { // Compute whether we should use checkPackageAccess or NOT
  2021   { // Compute whether we should use checkPackageAccess or NOT
  2039     methodOop method = instanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::checkPackageAccess_name(), vmSymbols::class_protectiondomain_signature());
  2022     Method* method = InstanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::checkPackageAccess_name(), vmSymbols::class_protectiondomain_signature());
  2040     _has_checkPackageAccess = (method != NULL);
  2023     _has_checkPackageAccess = (method != NULL);
  2041   }
  2024   }
  2042 }
  2025 }
  2043 
  2026 
  2044 // Tells if a given klass is a box (wrapper class, such as java.lang.Integer).
  2027 // Tells if a given klass is a box (wrapper class, such as java.lang.Integer).
  2045 // If so, returns the basic type it holds.  If not, returns T_OBJECT.
  2028 // If so, returns the basic type it holds.  If not, returns T_OBJECT.
  2046 BasicType SystemDictionary::box_klass_type(klassOop k) {
  2029 BasicType SystemDictionary::box_klass_type(Klass* k) {
  2047   assert(k != NULL, "");
  2030   assert(k != NULL, "");
  2048   for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
  2031   for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
  2049     if (_box_klasses[i] == k)
  2032     if (_box_klasses[i] == k)
  2050       return (BasicType)i;
  2033       return (BasicType)i;
  2051   }
  2034   }
  2052   return T_OBJECT;
  2035   return T_OBJECT;
  2053 }
       
  2054 
       
  2055 KlassHandle SystemDictionaryHandles::box_klass(BasicType t) {
       
  2056   if (t >= T_BOOLEAN && t <= T_VOID)
       
  2057     return KlassHandle(&SystemDictionary::_box_klasses[t], true);
       
  2058   else
       
  2059     return KlassHandle();
       
  2060 }
  2036 }
  2061 
  2037 
  2062 // Constraints on class loaders. The details of the algorithm can be
  2038 // Constraints on class loaders. The details of the algorithm can be
  2063 // found in the OOPSLA'98 paper "Dynamic Class Loading in the Java
  2039 // found in the OOPSLA'98 paper "Dynamic Class Loading in the Java
  2064 // Virtual Machine" by Sheng Liang and Gilad Bracha.  The basic idea is
  2040 // Virtual Machine" by Sheng Liang and Gilad Bracha.  The basic idea is
  2065 // that the system dictionary needs to maintain a set of contraints that
  2041 // that the system dictionary needs to maintain a set of contraints that
  2066 // must be satisfied by all classes in the dictionary.
  2042 // must be satisfied by all classes in the dictionary.
  2067 // if defining is true, then LinkageError if already in systemDictionary
  2043 // if defining is true, then LinkageError if already in systemDictionary
  2068 // if initiating loader, then ok if instanceKlass matches existing entry
  2044 // if initiating loader, then ok if InstanceKlass matches existing entry
  2069 
  2045 
  2070 void SystemDictionary::check_constraints(int d_index, unsigned int d_hash,
  2046 void SystemDictionary::check_constraints(int d_index, unsigned int d_hash,
  2071                                          instanceKlassHandle k,
  2047                                          instanceKlassHandle k,
  2072                                          Handle class_loader, bool defining,
  2048                                          Handle class_loader, bool defining,
  2073                                          TRAPS) {
  2049                                          TRAPS) {
  2074   const char *linkage_error = NULL;
  2050   const char *linkage_error = NULL;
  2075   {
  2051   {
  2076     Symbol*  name  = k->name();
  2052     Symbol*  name  = k->name();
       
  2053     ClassLoaderData *loader_data = class_loader_data(class_loader);
       
  2054 
  2077     MutexLocker mu(SystemDictionary_lock, THREAD);
  2055     MutexLocker mu(SystemDictionary_lock, THREAD);
  2078 
  2056 
  2079     klassOop check = find_class(d_index, d_hash, name, class_loader);
  2057     Klass* check = find_class(d_index, d_hash, name, loader_data);
  2080     if (check != (klassOop)NULL) {
  2058     if (check != (Klass*)NULL) {
  2081       // if different instanceKlass - duplicate class definition,
  2059       // if different InstanceKlass - duplicate class definition,
  2082       // else - ok, class loaded by a different thread in parallel,
  2060       // else - ok, class loaded by a different thread in parallel,
  2083       // we should only have found it if it was done loading and ok to use
  2061       // we should only have found it if it was done loading and ok to use
  2084       // system dictionary only holds instance classes, placeholders
  2062       // system dictionary only holds instance classes, placeholders
  2085       // also holds array classes
  2063       // also holds array classes
  2086 
  2064 
  2087       assert(check->klass_part()->oop_is_instance(), "noninstance in systemdictionary");
  2065       assert(check->oop_is_instance(), "noninstance in systemdictionary");
  2088       if ((defining == true) || (k() != check)) {
  2066       if ((defining == true) || (k() != check)) {
  2089         linkage_error = "loader (instance of  %s): attempted  duplicate class "
  2067         linkage_error = "loader (instance of  %s): attempted  duplicate class "
  2090           "definition for name: \"%s\"";
  2068           "definition for name: \"%s\"";
  2091       } else {
  2069       } else {
  2092         return;
  2070         return;
  2093       }
  2071       }
  2094     }
  2072     }
  2095 
  2073 
  2096 #ifdef ASSERT
  2074 #ifdef ASSERT
  2097     Symbol* ph_check = find_placeholder(name, class_loader);
  2075     Symbol* ph_check = find_placeholder(name, loader_data);
  2098     assert(ph_check == NULL || ph_check == name, "invalid symbol");
  2076     assert(ph_check == NULL || ph_check == name, "invalid symbol");
  2099 #endif
  2077 #endif
  2100 
  2078 
  2101     if (linkage_error == NULL) {
  2079     if (linkage_error == NULL) {
  2102       if (constraints()->check_or_update(k, class_loader, name) == false) {
  2080       if (constraints()->check_or_update(k, class_loader, name) == false) {
  2130                                          Handle class_loader,
  2108                                          Handle class_loader,
  2131                                          TRAPS) {
  2109                                          TRAPS) {
  2132   // Compile_lock prevents systemDictionary updates during compilations
  2110   // Compile_lock prevents systemDictionary updates during compilations
  2133   assert_locked_or_safepoint(Compile_lock);
  2111   assert_locked_or_safepoint(Compile_lock);
  2134   Symbol*  name  = k->name();
  2112   Symbol*  name  = k->name();
       
  2113   ClassLoaderData *loader_data = class_loader_data(class_loader);
  2135 
  2114 
  2136   {
  2115   {
  2137   MutexLocker mu1(SystemDictionary_lock, THREAD);
  2116   MutexLocker mu1(SystemDictionary_lock, THREAD);
  2138 
  2117 
  2139   // See whether biased locking is enabled and if so set it for this
  2118   // See whether biased locking is enabled and if so set it for this
  2140   // klass.
  2119   // klass.
  2141   // Note that this must be done past the last potential blocking
  2120   // Note that this must be done past the last potential blocking
  2142   // point / safepoint. We enable biased locking lazily using a
  2121   // point / safepoint. We enable biased locking lazily using a
  2143   // VM_Operation to iterate the SystemDictionary and installing the
  2122   // VM_Operation to iterate the SystemDictionary and installing the
  2144   // biasable mark word into each instanceKlass's prototype header.
  2123   // biasable mark word into each InstanceKlass's prototype header.
  2145   // To avoid race conditions where we accidentally miss enabling the
  2124   // To avoid race conditions where we accidentally miss enabling the
  2146   // optimization for one class in the process of being added to the
  2125   // optimization for one class in the process of being added to the
  2147   // dictionary, we must not safepoint after the test of
  2126   // dictionary, we must not safepoint after the test of
  2148   // BiasedLocking::enabled().
  2127   // BiasedLocking::enabled().
  2149   if (UseBiasedLocking && BiasedLocking::enabled()) {
  2128   if (UseBiasedLocking && BiasedLocking::enabled()) {
  2162   // All loaded classes get a unique ID.
  2141   // All loaded classes get a unique ID.
  2163   TRACE_INIT_ID(k);
  2142   TRACE_INIT_ID(k);
  2164 
  2143 
  2165   // Check for a placeholder. If there, remove it and make a
  2144   // Check for a placeholder. If there, remove it and make a
  2166   // new system dictionary entry.
  2145   // new system dictionary entry.
  2167   placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD);
  2146   placeholders()->find_and_remove(p_index, p_hash, name, loader_data, THREAD);
  2168   klassOop sd_check = find_class(d_index, d_hash, name, class_loader);
  2147   Klass* sd_check = find_class(d_index, d_hash, name, loader_data);
  2169   if (sd_check == NULL) {
  2148   if (sd_check == NULL) {
  2170     dictionary()->add_klass(name, class_loader, k);
  2149     dictionary()->add_klass(name, loader_data, k);
  2171     notice_modification();
  2150     notice_modification();
  2172   }
  2151   }
  2173 #ifdef ASSERT
  2152 #ifdef ASSERT
  2174   sd_check = find_class(d_index, d_hash, name, class_loader);
  2153   sd_check = find_class(d_index, d_hash, name, loader_data);
  2175   assert (sd_check != NULL, "should have entry in system dictionary");
  2154   assert (sd_check != NULL, "should have entry in system dictionary");
  2176 // Changed to allow PH to remain to complete class circularity checking
  2155 // Changed to allow PH to remain to complete class circularity checking
  2177 // while only one thread can define a class at one time, multiple
  2156 // while only one thread can define a class at one time, multiple
  2178 // classes can resolve the superclass for a class at one time,
  2157 // classes can resolve the superclass for a class at one time,
  2179 // and the placeholder is used to track that
  2158 // and the placeholder is used to track that
  2186 
  2165 
  2187 
  2166 
  2188 // Try to find a class name using the loader constraints.  The
  2167 // Try to find a class name using the loader constraints.  The
  2189 // loader constraints might know about a class that isn't fully loaded
  2168 // loader constraints might know about a class that isn't fully loaded
  2190 // yet and these will be ignored.
  2169 // yet and these will be ignored.
  2191 klassOop SystemDictionary::find_constrained_instance_or_array_klass(
  2170 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
  2192                     Symbol* class_name, Handle class_loader, TRAPS) {
  2171                     Symbol* class_name, Handle class_loader, TRAPS) {
  2193 
  2172 
  2194   // First see if it has been loaded directly.
  2173   // First see if it has been loaded directly.
  2195   // Force the protection domain to be null.  (This removes protection checks.)
  2174   // Force the protection domain to be null.  (This removes protection checks.)
  2196   Handle no_protection_domain;
  2175   Handle no_protection_domain;
  2197   klassOop klass = find_instance_or_array_klass(class_name, class_loader,
  2176   Klass* klass = find_instance_or_array_klass(class_name, class_loader,
  2198                                                 no_protection_domain, CHECK_NULL);
  2177                                                 no_protection_domain, CHECK_NULL);
  2199   if (klass != NULL)
  2178   if (klass != NULL)
  2200     return klass;
  2179     return klass;
  2201 
  2180 
  2202   // Now look to see if it has been loaded elsewhere, and is subject to
  2181   // Now look to see if it has been loaded elsewhere, and is subject to
  2203   // a loader constraint that would require this loader to return the
  2182   // a loader constraint that would require this loader to return the
  2204   // klass that is already loaded.
  2183   // klass that is already loaded.
  2205   if (FieldType::is_array(class_name)) {
  2184   if (FieldType::is_array(class_name)) {
  2206     // For array classes, their klassOops are not kept in the
  2185     // For array classes, their Klass*s are not kept in the
  2207     // constraint table. The element klassOops are.
  2186     // constraint table. The element Klass*s are.
  2208     FieldArrayInfo fd;
  2187     FieldArrayInfo fd;
  2209     BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(NULL));
  2188     BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(NULL));
  2210     if (t != T_OBJECT) {
  2189     if (t != T_OBJECT) {
  2211       klass = Universe::typeArrayKlassObj(t);
  2190       klass = Universe::typeArrayKlassObj(t);
  2212     } else {
  2191     } else {
  2229 
  2208 
  2230 bool SystemDictionary::add_loader_constraint(Symbol* class_name,
  2209 bool SystemDictionary::add_loader_constraint(Symbol* class_name,
  2231                                              Handle class_loader1,
  2210                                              Handle class_loader1,
  2232                                              Handle class_loader2,
  2211                                              Handle class_loader2,
  2233                                              Thread* THREAD) {
  2212                                              Thread* THREAD) {
       
  2213   ClassLoaderData* loader_data1 = class_loader_data(class_loader1);
       
  2214   ClassLoaderData* loader_data2 = class_loader_data(class_loader2);
       
  2215 
  2234   Symbol* constraint_name = NULL;
  2216   Symbol* constraint_name = NULL;
  2235   if (!FieldType::is_array(class_name)) {
  2217   if (!FieldType::is_array(class_name)) {
  2236     constraint_name = class_name;
  2218     constraint_name = class_name;
  2237   } else {
  2219   } else {
  2238     // For array classes, their klassOops are not kept in the
  2220     // For array classes, their Klass*s are not kept in the
  2239     // constraint table. The element classes are.
  2221     // constraint table. The element classes are.
  2240     FieldArrayInfo fd;
  2222     FieldArrayInfo fd;
  2241     BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(false));
  2223     BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(false));
  2242     // primitive types always pass
  2224     // primitive types always pass
  2243     if (t != T_OBJECT) {
  2225     if (t != T_OBJECT) {
  2244       return true;
  2226       return true;
  2245     } else {
  2227     } else {
  2246       constraint_name = fd.object_key();
  2228       constraint_name = fd.object_key();
  2247     }
  2229     }
  2248   }
  2230   }
  2249   unsigned int d_hash1 = dictionary()->compute_hash(constraint_name, class_loader1);
  2231   unsigned int d_hash1 = dictionary()->compute_hash(constraint_name, loader_data1);
  2250   int d_index1 = dictionary()->hash_to_index(d_hash1);
  2232   int d_index1 = dictionary()->hash_to_index(d_hash1);
  2251 
  2233 
  2252   unsigned int d_hash2 = dictionary()->compute_hash(constraint_name, class_loader2);
  2234   unsigned int d_hash2 = dictionary()->compute_hash(constraint_name, loader_data2);
  2253   int d_index2 = dictionary()->hash_to_index(d_hash2);
  2235   int d_index2 = dictionary()->hash_to_index(d_hash2);
  2254   {
  2236   {
  2255   MutexLocker mu_s(SystemDictionary_lock, THREAD);
  2237   MutexLocker mu_s(SystemDictionary_lock, THREAD);
  2256 
  2238 
  2257   // Better never do a GC while we're holding these oops
  2239   // Better never do a GC while we're holding these oops
  2258   No_Safepoint_Verifier nosafepoint;
  2240   No_Safepoint_Verifier nosafepoint;
  2259 
  2241 
  2260   klassOop klass1 = find_class(d_index1, d_hash1, constraint_name, class_loader1);
  2242   Klass* klass1 = find_class(d_index1, d_hash1, constraint_name, loader_data1);
  2261   klassOop klass2 = find_class(d_index2, d_hash2, constraint_name, class_loader2);
  2243   Klass* klass2 = find_class(d_index2, d_hash2, constraint_name, loader_data2);
  2262   return constraints()->add_entry(constraint_name, klass1, class_loader1,
  2244   return constraints()->add_entry(constraint_name, klass1, class_loader1,
  2263                                   klass2, class_loader2);
  2245                                   klass2, class_loader2);
  2264   }
  2246   }
  2265 }
  2247 }
  2266 
  2248 
  2271   int index = resolution_errors()->hash_to_index(hash);
  2253   int index = resolution_errors()->hash_to_index(hash);
  2272   {
  2254   {
  2273     MutexLocker ml(SystemDictionary_lock, Thread::current());
  2255     MutexLocker ml(SystemDictionary_lock, Thread::current());
  2274     resolution_errors()->add_entry(index, hash, pool, which, error);
  2256     resolution_errors()->add_entry(index, hash, pool, which, error);
  2275   }
  2257   }
       
  2258 }
       
  2259 
       
  2260 // Delete a resolution error for RedefineClasses for a constant pool is going away
       
  2261 void SystemDictionary::delete_resolution_error(ConstantPool* pool) {
       
  2262   resolution_errors()->delete_entry(pool);
  2276 }
  2263 }
  2277 
  2264 
  2278 // Lookup resolution error table. Returns error if found, otherwise NULL.
  2265 // Lookup resolution error table. Returns error if found, otherwise NULL.
  2279 Symbol* SystemDictionary::find_resolution_error(constantPoolHandle pool, int which) {
  2266 Symbol* SystemDictionary::find_resolution_error(constantPoolHandle pool, int which) {
  2280   unsigned int hash = resolution_errors()->compute_hash(pool, which);
  2267   unsigned int hash = resolution_errors()->compute_hash(pool, which);
  2374 
  2361 
  2375   unsigned int hash  = invoke_method_table()->compute_hash(signature, iid);
  2362   unsigned int hash  = invoke_method_table()->compute_hash(signature, iid);
  2376   int          index = invoke_method_table()->hash_to_index(hash);
  2363   int          index = invoke_method_table()->hash_to_index(hash);
  2377   SymbolPropertyEntry* spe = invoke_method_table()->find_entry(index, hash, signature, iid);
  2364   SymbolPropertyEntry* spe = invoke_method_table()->find_entry(index, hash, signature, iid);
  2378   methodHandle m;
  2365   methodHandle m;
  2379   if (spe == NULL || spe->property_oop() == NULL) {
  2366   if (spe == NULL || spe->method() == NULL) {
  2380     spe = NULL;
  2367     spe = NULL;
  2381     // Must create lots of stuff here, but outside of the SystemDictionary lock.
  2368     // Must create lots of stuff here, but outside of the SystemDictionary lock.
  2382     m = methodOopDesc::make_method_handle_intrinsic(iid, signature, CHECK_(empty));
  2369     m = Method::make_method_handle_intrinsic(iid, signature, CHECK_(empty));
  2383     CompileBroker::compile_method(m, InvocationEntryBci, CompLevel_highest_tier,
  2370     CompileBroker::compile_method(m, InvocationEntryBci, CompLevel_highest_tier,
  2384                                   methodHandle(), CompileThreshold, "MH", CHECK_(empty));
  2371                                   methodHandle(), CompileThreshold, "MH", CHECK_(empty));
  2385 
  2372 
  2386     // Now grab the lock.  We might have to throw away the new method,
  2373     // Now grab the lock.  We might have to throw away the new method,
  2387     // if a racing thread has managed to install one at the same time.
  2374     // if a racing thread has managed to install one at the same time.
  2388     {
  2375     {
  2389       MutexLocker ml(SystemDictionary_lock, THREAD);
  2376       MutexLocker ml(SystemDictionary_lock, THREAD);
  2390       spe = invoke_method_table()->find_entry(index, hash, signature, iid);
  2377       spe = invoke_method_table()->find_entry(index, hash, signature, iid);
  2391       if (spe == NULL)
  2378       if (spe == NULL)
  2392         spe = invoke_method_table()->add_entry(index, hash, signature, iid);
  2379         spe = invoke_method_table()->add_entry(index, hash, signature, iid);
  2393       if (spe->property_oop() == NULL)
  2380       if (spe->method() == NULL)
  2394         spe->set_property_oop(m());
  2381         spe->set_method(m());
  2395     }
  2382     }
  2396   }
  2383   }
  2397 
  2384 
  2398   assert(spe != NULL && spe->property_oop() != NULL, "");
  2385   assert(spe != NULL && spe->method() != NULL, "");
  2399   m = methodOop(spe->property_oop());
  2386   return spe->method();
  2400   assert(m->is_method(), "");
       
  2401 
       
  2402   return m;
       
  2403 }
  2387 }
  2404 
  2388 
  2405 // Helper for unpacking the return value from linkMethod and linkCallSite.
  2389 // Helper for unpacking the return value from linkMethod and linkCallSite.
  2406 static methodHandle unpack_method_and_appendix(Handle mname,
  2390 static methodHandle unpack_method_and_appendix(Handle mname,
  2407                                                objArrayHandle appendix_box,
  2391                                                objArrayHandle appendix_box,
  2408                                                Handle* appendix_result,
  2392                                                Handle* appendix_result,
  2409                                                TRAPS) {
  2393                                                TRAPS) {
  2410   methodHandle empty;
  2394   methodHandle empty;
  2411   if (mname.not_null()) {
  2395   if (mname.not_null()) {
  2412     oop vmtarget = java_lang_invoke_MemberName::vmtarget(mname());
  2396     Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(mname());
  2413     if (vmtarget != NULL && vmtarget->is_method()) {
  2397     if (vmtarget != NULL && vmtarget->is_method()) {
  2414       methodOop m = methodOop(vmtarget);
  2398       Method* m = (Method*)vmtarget;
  2415       oop appendix = appendix_box->obj_at(0);
  2399       oop appendix = appendix_box->obj_at(0);
  2416       if (TraceMethodHandles) {
  2400       if (TraceMethodHandles) {
  2417     #ifndef PRODUCT
  2401     #ifndef PRODUCT
  2418         tty->print("Linked method="INTPTR_FORMAT": ", m);
  2402         tty->print("Linked method="INTPTR_FORMAT": ", m);
  2419         m->print();
  2403         m->print();
  2441     SystemDictionary::find_method_handle_type(signature, accessing_klass, CHECK_(empty));
  2425     SystemDictionary::find_method_handle_type(signature, accessing_klass, CHECK_(empty));
  2442   if (false) {  // FIXME: Decide if the Java upcall should resolve signatures.
  2426   if (false) {  // FIXME: Decide if the Java upcall should resolve signatures.
  2443     method_type = java_lang_String::create_from_symbol(signature, CHECK_(empty));
  2427     method_type = java_lang_String::create_from_symbol(signature, CHECK_(empty));
  2444   }
  2428   }
  2445 
  2429 
  2446   KlassHandle  mh_klass = SystemDictionaryHandles::MethodHandle_klass();
  2430   KlassHandle  mh_klass = SystemDictionary::MethodHandle_klass();
  2447   int ref_kind = JVM_REF_invokeVirtual;
  2431   int ref_kind = JVM_REF_invokeVirtual;
  2448   Handle name_str = StringTable::intern(name, CHECK_(empty));
  2432   Handle name_str = StringTable::intern(name, CHECK_(empty));
  2449   objArrayHandle appendix_box = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1, CHECK_(empty));
  2433   objArrayHandle appendix_box = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1, CHECK_(empty));
  2450   assert(appendix_box->obj_at(0) == NULL, "");
  2434   assert(appendix_box->obj_at(0) == NULL, "");
  2451 
  2435 
  2478   Handle empty;
  2462   Handle empty;
  2479   vmIntrinsics::ID null_iid = vmIntrinsics::_none;  // distinct from all method handle invoker intrinsics
  2463   vmIntrinsics::ID null_iid = vmIntrinsics::_none;  // distinct from all method handle invoker intrinsics
  2480   unsigned int hash  = invoke_method_table()->compute_hash(signature, null_iid);
  2464   unsigned int hash  = invoke_method_table()->compute_hash(signature, null_iid);
  2481   int          index = invoke_method_table()->hash_to_index(hash);
  2465   int          index = invoke_method_table()->hash_to_index(hash);
  2482   SymbolPropertyEntry* spe = invoke_method_table()->find_entry(index, hash, signature, null_iid);
  2466   SymbolPropertyEntry* spe = invoke_method_table()->find_entry(index, hash, signature, null_iid);
  2483   if (spe != NULL && spe->property_oop() != NULL) {
  2467   if (spe != NULL && spe->method_type() != NULL) {
  2484     assert(java_lang_invoke_MethodType::is_instance(spe->property_oop()), "");
  2468     assert(java_lang_invoke_MethodType::is_instance(spe->method_type()), "");
  2485     return Handle(THREAD, spe->property_oop());
  2469     return Handle(THREAD, spe->method_type());
  2486   } else if (THREAD->is_Compiler_thread()) {
  2470   } else if (THREAD->is_Compiler_thread()) {
  2487     warning("SystemDictionary::find_method_handle_type called from compiler thread");  // FIXME
  2471     warning("SystemDictionary::find_method_handle_type called from compiler thread");  // FIXME
  2488     return Handle();  // do not attempt from within compiler, unless it was cached
  2472     return Handle();  // do not attempt from within compiler, unless it was cached
  2489   }
  2473   }
  2490 
  2474 
  2502       mirror = ss.as_java_mirror(class_loader, protection_domain,
  2486       mirror = ss.as_java_mirror(class_loader, protection_domain,
  2503                                  SignatureStream::ReturnNull, CHECK_(empty));
  2487                                  SignatureStream::ReturnNull, CHECK_(empty));
  2504       if (mirror == NULL) {
  2488       if (mirror == NULL) {
  2505         // fall back from BCP to accessing_klass
  2489         // fall back from BCP to accessing_klass
  2506         if (accessing_klass.not_null()) {
  2490         if (accessing_klass.not_null()) {
  2507           class_loader      = Handle(THREAD, instanceKlass::cast(accessing_klass())->class_loader());
  2491           class_loader      = Handle(THREAD, InstanceKlass::cast(accessing_klass())->class_loader());
  2508           protection_domain = Handle(THREAD, instanceKlass::cast(accessing_klass())->protection_domain());
  2492           protection_domain = Handle(THREAD, InstanceKlass::cast(accessing_klass())->protection_domain());
  2509         }
  2493         }
  2510         is_on_bcp = false;
  2494         is_on_bcp = false;
  2511       }
  2495       }
  2512     }
  2496     }
  2513     if (!is_on_bcp) {
  2497     if (!is_on_bcp) {
  2520     else
  2504     else
  2521       pts->obj_at_put(arg++, mirror);
  2505       pts->obj_at_put(arg++, mirror);
  2522 
  2506 
  2523     // Check accessibility.
  2507     // Check accessibility.
  2524     if (ss.is_object() && accessing_klass.not_null()) {
  2508     if (ss.is_object() && accessing_klass.not_null()) {
  2525       klassOop sel_klass = java_lang_Class::as_klassOop(mirror);
  2509       Klass* sel_klass = java_lang_Class::as_Klass(mirror);
  2526       mirror = NULL;  // safety
  2510       mirror = NULL;  // safety
  2527       // Emulate constantPoolOopDesc::verify_constant_pool_resolve.
  2511       // Emulate ConstantPool::verify_constant_pool_resolve.
  2528       if (Klass::cast(sel_klass)->oop_is_objArray())
  2512       if (Klass::cast(sel_klass)->oop_is_objArray())
  2529         sel_klass = objArrayKlass::cast(sel_klass)->bottom_klass();
  2513         sel_klass = objArrayKlass::cast(sel_klass)->bottom_klass();
  2530       if (Klass::cast(sel_klass)->oop_is_instance()) {
  2514       if (Klass::cast(sel_klass)->oop_is_instance()) {
  2531         KlassHandle sel_kh(THREAD, sel_klass);
  2515         KlassHandle sel_kh(THREAD, sel_klass);
  2532         LinkResolver::check_klass_accessability(accessing_klass, sel_kh, CHECK_(empty));
  2516         LinkResolver::check_klass_accessability(accessing_klass, sel_kh, CHECK_(empty));
  2550     // We can cache this MethodType inside the JVM.
  2534     // We can cache this MethodType inside the JVM.
  2551     MutexLocker ml(SystemDictionary_lock, THREAD);
  2535     MutexLocker ml(SystemDictionary_lock, THREAD);
  2552     spe = invoke_method_table()->find_entry(index, hash, signature, null_iid);
  2536     spe = invoke_method_table()->find_entry(index, hash, signature, null_iid);
  2553     if (spe == NULL)
  2537     if (spe == NULL)
  2554       spe = invoke_method_table()->add_entry(index, hash, signature, null_iid);
  2538       spe = invoke_method_table()->add_entry(index, hash, signature, null_iid);
  2555     if (spe->property_oop() == NULL) {
  2539     if (spe->method_type() == NULL) {
  2556       spe->set_property_oop(method_type());
  2540       spe->set_method_type(method_type());
  2557     }
  2541     }
  2558   }
  2542   }
  2559 
  2543 
  2560   // report back to the caller with the MethodType
  2544   // report back to the caller with the MethodType
  2561   return method_type;
  2545   return method_type;
  2719   guarantee(constraints() != NULL, "Verify of loader constraints failed");
  2703   guarantee(constraints() != NULL, "Verify of loader constraints failed");
  2720   constraints()->verify(dictionary(), placeholders());
  2704   constraints()->verify(dictionary(), placeholders());
  2721 }
  2705 }
  2722 
  2706 
  2723 
  2707 
  2724 void SystemDictionary::verify_obj_klass_present(Handle obj,
  2708 void SystemDictionary::verify_obj_klass_present(Symbol* class_name,
  2725                                                 Symbol* class_name,
  2709                                                 ClassLoaderData* loader_data) {
  2726                                                 Handle class_loader) {
       
  2727   GCMutexLocker mu(SystemDictionary_lock);
  2710   GCMutexLocker mu(SystemDictionary_lock);
  2728   Symbol* name;
  2711   Symbol* name;
  2729 
  2712 
  2730   klassOop probe = find_class(class_name, class_loader);
  2713   Klass* probe = find_class(class_name, loader_data);
  2731   if (probe == NULL) {
  2714   if (probe == NULL) {
  2732     probe = SystemDictionary::find_shared_class(class_name);
  2715     probe = SystemDictionary::find_shared_class(class_name);
  2733     if (probe == NULL) {
  2716     if (probe == NULL) {
  2734       name = find_placeholder(class_name, class_loader);
  2717       name = find_placeholder(class_name, loader_data);
  2735     }
  2718     }
  2736   }
  2719   }
  2737   guarantee(probe != NULL || name != NULL,
  2720   guarantee(probe != NULL || name != NULL,
  2738             "Loaded klasses should be in SystemDictionary");
  2721             "Loaded klasses should be in SystemDictionary");
  2739 }
  2722 }
  2749   static int class_size;      // size of class objects in words
  2732   static int class_size;      // size of class objects in words
  2750   static int method_size;     // size of method objects in words
  2733   static int method_size;     // size of method objects in words
  2751   static int debug_size;      // size of debug info in methods
  2734   static int debug_size;      // size of debug info in methods
  2752   static int methoddata_size; // size of methodData objects in words
  2735   static int methoddata_size; // size of methodData objects in words
  2753 
  2736 
  2754   static void do_class(klassOop k) {
  2737   static void do_class(Klass* k) {
  2755     nclasses++;
  2738     nclasses++;
  2756     class_size += k->size();
  2739     class_size += k->size();
  2757     if (k->klass_part()->oop_is_instance()) {
  2740     if (k->oop_is_instance()) {
  2758       instanceKlass* ik = (instanceKlass*)k->klass_part();
  2741       InstanceKlass* ik = (InstanceKlass*)k;
  2759       class_size += ik->methods()->size();
  2742       class_size += ik->methods()->size();
  2760       class_size += ik->constants()->size();
  2743       class_size += ik->constants()->size();
  2761       class_size += ik->local_interfaces()->size();
  2744       class_size += ik->local_interfaces()->size();
  2762       class_size += ik->transitive_interfaces()->size();
  2745       class_size += ik->transitive_interfaces()->size();
  2763       // We do not have to count implementors, since we only store one!
  2746       // We do not have to count implementors, since we only store one!
  2764       class_size += ik->fields()->length();
  2747       // SSS: How should these be accounted now that they have moved?
  2765     }
  2748       // class_size += ik->fields()->length();
  2766   }
  2749     }
  2767 
  2750   }
  2768   static void do_method(methodOop m) {
  2751 
       
  2752   static void do_method(Method* m) {
  2769     nmethods++;
  2753     nmethods++;
  2770     method_size += m->size();
  2754     method_size += m->size();
  2771     // class loader uses same objArray for empty vectors, so don't count these
  2755     // class loader uses same objArray for empty vectors, so don't count these
  2772     if (m->has_stackmap_table()) {
  2756     if (m->has_stackmap_table()) {
  2773       method_size += m->stackmap_data()->size();
  2757       method_size += m->stackmap_data()->size();
  2774     }
  2758     }
  2775 
  2759 
  2776     methodDataOop mdo = m->method_data();
  2760     MethodData* mdo = m->method_data();
  2777     if (mdo != NULL) {
  2761     if (mdo != NULL) {
  2778       nmethoddata++;
  2762       nmethoddata++;
  2779       methoddata_size += mdo->size();
  2763       methoddata_size += mdo->size();
  2780     }
  2764     }
  2781   }
  2765   }
  2834     _number_of_bytecodes      = 0;
  2818     _number_of_bytecodes      = 0;
  2835     for (int i = 0; i < max_parameter_size             ; i++) _parameter_size_profile[i] = 0;
  2819     for (int i = 0; i < max_parameter_size             ; i++) _parameter_size_profile[i] = 0;
  2836     for (int j = 0; j < Bytecodes::number_of_java_codes; j++) _bytecodes_profile     [j] = 0;
  2820     for (int j = 0; j < Bytecodes::number_of_java_codes; j++) _bytecodes_profile     [j] = 0;
  2837   };
  2821   };
  2838 
  2822 
  2839   static void do_method(methodOop m) {
  2823   static void do_method(Method* m) {
  2840     _number_of_methods++;
  2824     _number_of_methods++;
  2841     // collect flag info
  2825     // collect flag info
  2842     if (m->is_final()       ) _number_of_final_methods++;
  2826     if (m->is_final()       ) _number_of_final_methods++;
  2843     if (m->is_static()      ) _number_of_static_methods++;
  2827     if (m->is_static()      ) _number_of_static_methods++;
  2844     if (m->is_native()      ) _number_of_native_methods++;
  2828     if (m->is_native()      ) _number_of_native_methods++;