< prev index next >

src/hotspot/share/classfile/systemDictionary.cpp

Print this page


   1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


 142   JavaCalls::call_static(&result,
 143                          class_loader_klass,
 144                          vmSymbols::getPlatformClassLoader_name(),
 145                          vmSymbols::void_classloader_signature(),
 146                          CHECK);
 147 
 148   _java_platform_loader = (oop)result.get_jobject();
 149 
 150   CDS_ONLY(SystemDictionaryShared::initialize(CHECK);)
 151 }
 152 
 153 ClassLoaderData* SystemDictionary::register_loader(Handle class_loader, TRAPS) {
 154   if (class_loader() == NULL) return ClassLoaderData::the_null_class_loader_data();
 155   return ClassLoaderDataGraph::find_or_create(class_loader, THREAD);
 156 }
 157 
 158 // ----------------------------------------------------------------------------
 159 // Parallel class loading check
 160 
 161 bool SystemDictionary::is_parallelCapable(Handle class_loader) {
 162   if (UnsyncloadClass || class_loader.is_null()) return true;
 163   if (AlwaysLockClassLoader) return false;
 164   return java_lang_ClassLoader::parallelCapable(class_loader());
 165 }
 166 // ----------------------------------------------------------------------------
 167 // ParallelDefineClass flag does not apply to bootclass loader
 168 bool SystemDictionary::is_parallelDefine(Handle class_loader) {
 169    if (class_loader.is_null()) return false;
 170    if (AllowParallelDefineClass && java_lang_ClassLoader::parallelCapable(class_loader())) {
 171      return true;
 172    }
 173    return false;
 174 }
 175 
 176 // Returns true if the passed class loader is the builtin application class loader
 177 // or a custom system class loader. A customer system class loader can be
 178 // specified via -Djava.system.class.loader.
 179 bool SystemDictionary::is_system_class_loader(oop class_loader) {
 180   if (class_loader == NULL) {
 181     return false;
 182   }


 486 
 487     Symbol*  kn = klass->name();
 488     unsigned int d_hash = dictionary->compute_hash(kn);
 489 
 490     MutexLocker mu(SystemDictionary_lock, THREAD);
 491     int d_index = dictionary->hash_to_index(d_hash);
 492     dictionary->add_protection_domain(d_index, d_hash, klass,
 493                                       protection_domain, THREAD);
 494   }
 495 }
 496 
 497 // We only get here if this thread finds that another thread
 498 // has already claimed the placeholder token for the current operation,
 499 // but that other thread either never owned or gave up the
 500 // object lock
 501 // Waits on SystemDictionary_lock to indicate placeholder table updated
 502 // On return, caller must recheck placeholder table state
 503 //
 504 // We only get here if
 505 //  1) custom classLoader, i.e. not bootstrap classloader
 506 //  2) UnsyncloadClass not set
 507 //  3) custom classLoader has broken the class loader objectLock
 508 //     so another thread got here in parallel
 509 //
 510 // lockObject must be held.
 511 // Complicated dance due to lock ordering:
 512 // Must first release the classloader object lock to
 513 // allow initial definer to complete the class definition
 514 // and to avoid deadlock
 515 // Reclaim classloader lock object with same original recursion count
 516 // Must release SystemDictionary_lock after notify, since
 517 // class loader lock must be claimed before SystemDictionary_lock
 518 // to prevent deadlocks
 519 //
 520 // The notify allows applications that did an untimed wait() on
 521 // the classloader object lock to not hang.
 522 void SystemDictionary::double_lock_wait(Handle lockObject, TRAPS) {
 523   assert_lock_strong(SystemDictionary_lock);
 524 
 525   bool calledholdinglock
 526       = ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, lockObject);
 527   assert(calledholdinglock,"must hold lock for notify");


 577  if (!class_loader.is_null() && is_parallelCapable(class_loader)) {
 578     MutexLocker mu(SystemDictionary_lock, THREAD);
 579     // Check if classloading completed while we were loading superclass or waiting
 580     return find_class(d_hash, name, dictionary);
 581   }
 582 
 583   // must loop to both handle other placeholder updates
 584   // and spurious notifications
 585   bool super_load_in_progress = true;
 586   PlaceholderEntry* placeholder;
 587   while (super_load_in_progress) {
 588     MutexLocker mu(SystemDictionary_lock, THREAD);
 589     // Check if classloading completed while we were loading superclass or waiting
 590     InstanceKlass* check = find_class(d_hash, name, dictionary);
 591     if (check != NULL) {
 592       // Klass is already loaded, so just return it
 593       return check;
 594     } else {
 595       placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data);
 596       if (placeholder && placeholder->super_load_in_progress() ){
 597         // Before UnsyncloadClass:
 598         // We only get here if the application has released the
 599         // classloader lock when another thread was in the middle of loading a
 600         // superclass/superinterface for this class, and now
 601         // this thread is also trying to load this class.
 602         // To minimize surprises, the first thread that started to
 603         // load a class should be the one to complete the loading
 604         // with the classfile it initially expected.
 605         // This logic has the current thread wait once it has done
 606         // all the superclass/superinterface loading it can, until
 607         // the original thread completes the class loading or fails
 608         // If it completes we will use the resulting InstanceKlass
 609         // which we will find below in the systemDictionary.
 610         // We also get here for parallel bootstrap classloader
 611         if (class_loader.is_null()) {
 612           SystemDictionary_lock->wait();
 613         } else {
 614           double_lock_wait(lockObject, THREAD);
 615         }
 616       } else {
 617         // If not in SD and not in PH, other thread's load must have failed


 670   Dictionary* dictionary = loader_data->dictionary();
 671   unsigned int d_hash = dictionary->compute_hash(name);
 672 
 673   // Do lookup to see if class already exist and the protection domain
 674   // has the right access
 675   // This call uses find which checks protection domain already matches
 676   // All subsequent calls use find_class, and set has_loaded_class so that
 677   // before we return a result we call out to java to check for valid protection domain
 678   // to allow returning the Klass* and add it to the pd_set if it is valid
 679   {
 680     Klass* probe = dictionary->find(d_hash, name, protection_domain);
 681     if (probe != NULL) return probe;
 682   }
 683 
 684   // Non-bootstrap class loaders will call out to class loader and
 685   // define via jvm/jni_DefineClass which will acquire the
 686   // class loader object lock to protect against multiple threads
 687   // defining the class in parallel by accident.
 688   // This lock must be acquired here so the waiter will find
 689   // any successful result in the SystemDictionary and not attempt
 690   // the define
 691   // ParallelCapable Classloaders and the bootstrap classloader,
 692   // or all classloaders with UnsyncloadClass do not acquire lock here
 693   bool DoObjectLock = true;
 694   if (is_parallelCapable(class_loader)) {
 695     DoObjectLock = false;
 696   }
 697 
 698   unsigned int p_hash = placeholders()->compute_hash(name);
 699   int p_index = placeholders()->hash_to_index(p_hash);
 700 
 701   // Class is not in SystemDictionary so we have to do loading.
 702   // Make sure we are synchronized on the class loader before we proceed
 703   Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
 704   check_loader_lock_contention(lockObject, THREAD);
 705   ObjectLocker ol(lockObject, THREAD, DoObjectLock);
 706 
 707   // Check again (after locking) if class already exist in SystemDictionary
 708   bool class_has_been_loaded   = false;
 709   bool super_load_in_progress  = false;
 710   bool havesupername = false;
 711   InstanceKlass* k = NULL;
 712   PlaceholderEntry* placeholder;


 748 
 749   bool throw_circularity_error = false;
 750   if (!class_has_been_loaded) {
 751     bool load_instance_added = false;
 752 
 753     // add placeholder entry to record loading instance class
 754     // Five cases:
 755     // All cases need to prevent modifying bootclasssearchpath
 756     // in parallel with a classload of same classname
 757     // Redefineclasses uses existence of the placeholder for the duration
 758     // of the class load to prevent concurrent redefinition of not completely
 759     // defined classes.
 760     // case 1. traditional classloaders that rely on the classloader object lock
 761     //   - no other need for LOAD_INSTANCE
 762     // case 2. traditional classloaders that break the classloader object lock
 763     //    as a deadlock workaround. Detection of this case requires that
 764     //    this check is done while holding the classloader object lock,
 765     //    and that lock is still held when calling classloader's loadClass.
 766     //    For these classloaders, we ensure that the first requestor
 767     //    completes the load and other requestors wait for completion.
 768     // case 3. UnsyncloadClass - don't use objectLocker
 769     //    With this flag, we allow parallel classloading of a
 770     //    class/classloader pair
 771     // case4. Bootstrap classloader - don't own objectLocker
 772     //    This classloader supports parallelism at the classloader level,
 773     //    but only allows a single load of a class/classloader pair.
 774     //    No performance benefit and no deadlock issues.
 775     // case 5. parallelCapable user level classloaders - without objectLocker
 776     //    Allow parallel classloading of a class/classloader pair
 777 
 778     {
 779       MutexLocker mu(SystemDictionary_lock, THREAD);
 780       if (class_loader.is_null() || !is_parallelCapable(class_loader)) {
 781         PlaceholderEntry* oldprobe = placeholders()->get_entry(p_index, p_hash, name, loader_data);
 782         if (oldprobe) {
 783           // only need check_seen_thread once, not on each loop
 784           // 6341374 java/lang/Instrument with -Xcomp
 785           if (oldprobe->check_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE)) {
 786             throw_circularity_error = true;
 787           } else {
 788             // case 1: traditional: should never see load_in_progress.
 789             while (!class_has_been_loaded && oldprobe && oldprobe->instance_load_in_progress()) {
 790 
 791               // case 4: bootstrap classloader: prevent futile classloading,
 792               // wait on first requestor
 793               if (class_loader.is_null()) {
 794                 SystemDictionary_lock->wait();
 795               } else {
 796               // case 2: traditional with broken classloader lock. wait on first
 797               // requestor.
 798                 double_lock_wait(lockObject, THREAD);
 799               }
 800               // Check if classloading completed while we were waiting
 801               InstanceKlass* check = find_class(d_hash, name, dictionary);
 802               if (check != NULL) {
 803                 // Klass is already loaded, so just return it
 804                 k = check;
 805                 class_has_been_loaded = true;
 806               }
 807               // check if other thread failed to load and cleaned up
 808               oldprobe = placeholders()->get_entry(p_index, p_hash, name, loader_data);
 809             }
 810           }
 811         }
 812       }
 813       // All cases: add LOAD_INSTANCE holding SystemDictionary_lock
 814       // case 3: UnsyncloadClass || case 5: parallelCapable: allow competing threads to try
 815       // LOAD_INSTANCE in parallel
 816 
 817       if (!throw_circularity_error && !class_has_been_loaded) {
 818         PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, NULL, THREAD);
 819         load_instance_added = true;
 820         // For class loaders that do not acquire the classloader object lock,
 821         // if they did not catch another thread holding LOAD_INSTANCE,
 822         // need a check analogous to the acquire ObjectLocker/find_class
 823         // i.e. now that we hold the LOAD_INSTANCE token on loading this class/CL
 824         // one final check if the load has already completed
 825         // class loaders holding the ObjectLock shouldn't find the class here
 826         InstanceKlass* check = find_class(d_hash, name, dictionary);
 827         if (check != NULL) {
 828         // Klass is already loaded, so return it after checking/adding protection domain
 829           k = check;
 830           class_has_been_loaded = true;
 831         }
 832       }
 833     }
 834 
 835     // must throw error outside of owning lock
 836     if (throw_circularity_error) {
 837       assert(!HAS_PENDING_EXCEPTION && load_instance_added == false,"circularity error cleanup");
 838       ResourceMark rm(THREAD);
 839       THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), name->as_C_string());
 840     }
 841 
 842     if (!class_has_been_loaded) {
 843 
 844       // Do actual loading
 845       k = load_instance_class(name, class_loader, THREAD);
 846 
 847       // For UnsyncloadClass only
 848       // If they got a linkageError, check if a parallel class load succeeded.
 849       // If it did, then for bytecode resolution the specification requires
 850       // that we return the same result we did for the other thread, i.e. the
 851       // successfully loaded InstanceKlass
 852       // Should not get here for classloaders that support parallelism
 853       // with the new cleaner mechanism, even with AllowParallelDefineClass
 854       // Bootstrap goes through here to allow for an extra guarantee check
 855       if (UnsyncloadClass || (class_loader.is_null())) {
 856         if (k == NULL && HAS_PENDING_EXCEPTION
 857           && PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
 858           MutexLocker mu(SystemDictionary_lock, THREAD);
 859           InstanceKlass* check = find_class(d_hash, name, dictionary);
 860           if (check != NULL) {
 861             // Klass is already loaded, so just use it
 862             k = check;
 863             CLEAR_PENDING_EXCEPTION;
 864             guarantee((!class_loader.is_null()), "dup definition for bootstrap loader?");
 865           }
 866         }
 867       }
 868 
 869       // If everything was OK (no exceptions, no null return value), and
 870       // class_loader is NOT the defining loader, do a little more bookkeeping.
 871       if (!HAS_PENDING_EXCEPTION && k != NULL &&
 872         k->class_loader() != class_loader()) {
 873 
 874         check_constraints(d_hash, k, class_loader, false, THREAD);
 875 


1080 // the class until we have parsed the stream.
1081 
1082 InstanceKlass* SystemDictionary::resolve_from_stream(Symbol* class_name,
1083                                                      Handle class_loader,
1084                                                      Handle protection_domain,
1085                                                      ClassFileStream* st,
1086                                                      TRAPS) {
1087 #if INCLUDE_CDS
1088   ResourceMark rm(THREAD);
1089   if (DumpSharedSpaces && !class_loader.is_null() &&
1090       !UseAppCDS && strcmp(class_name->as_C_string(), "Unnamed") != 0) {
1091     // If AppCDS is not enabled, don't define the class at dump time (except for the "Unnamed"
1092     // class, which is used by MethodHandles).
1093     THROW_MSG_NULL(vmSymbols::java_lang_ClassNotFoundException(), class_name->as_C_string());
1094   }
1095 #endif
1096 
1097   HandleMark hm(THREAD);
1098 
1099   // Classloaders that support parallelism, e.g. bootstrap classloader,
1100   // or all classloaders with UnsyncloadClass do not acquire lock here
1101   bool DoObjectLock = true;
1102   if (is_parallelCapable(class_loader)) {
1103     DoObjectLock = false;
1104   }
1105 
1106   ClassLoaderData* loader_data = register_loader(class_loader, CHECK_NULL);
1107 
1108   // Make sure we are synchronized on the class loader before we proceed
1109   Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
1110   check_loader_lock_contention(lockObject, THREAD);
1111   ObjectLocker ol(lockObject, THREAD, DoObjectLock);
1112 
1113   assert(st != NULL, "invariant");
1114 
1115   // Parse the stream and create a klass.
1116   // Note that we do this even though this klass might
1117   // already be present in the SystemDictionary, otherwise we would not
1118   // throw potential ClassFormatErrors.
1119  InstanceKlass* k = NULL;
1120 


1544     PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
1545                                ClassLoader::perf_app_classload_selftime(),
1546                                ClassLoader::perf_app_classload_count(),
1547                                jt->get_thread_stat()->perf_recursion_counts_addr(),
1548                                jt->get_thread_stat()->perf_timers_addr(),
1549                                PerfClassTraceTime::CLASS_LOAD);
1550 
1551     Handle s = java_lang_String::create_from_symbol(class_name, CHECK_NULL);
1552     // Translate to external class name format, i.e., convert '/' chars to '.'
1553     Handle string = java_lang_String::externalize_classname(s, CHECK_NULL);
1554 
1555     JavaValue result(T_OBJECT);
1556 
1557     InstanceKlass* spec_klass = SystemDictionary::ClassLoader_klass();
1558 
1559     // Call public unsynchronized loadClass(String) directly for all class loaders
1560     // for parallelCapable class loaders. JDK >=7, loadClass(String, boolean) will
1561     // acquire a class-name based lock rather than the class loader object lock.
1562     // JDK < 7 already acquire the class loader lock in loadClass(String, boolean),
1563     // so the call to loadClassInternal() was not required.
1564     //
1565     // UnsyncloadClass flag means both call loadClass(String) and do
1566     // not acquire the class loader lock even for class loaders that are
1567     // not parallelCapable. This was a risky transitional
1568     // flag for diagnostic purposes only. It is risky to call
1569     // custom class loaders without synchronization.
1570     // WARNING If a custom class loader does NOT synchronizer findClass, or callers of
1571     // findClass, the UnsyncloadClass flag risks unexpected timing bugs in the field.
1572     // Do NOT assume this will be supported in future releases.
1573     //
1574     // Added MustCallLoadClassInternal in case we discover in the field
1575     // a customer that counts on this call
1576     if (MustCallLoadClassInternal && has_loadClassInternal()) {
1577       JavaCalls::call_special(&result,
1578                               class_loader,
1579                               spec_klass,
1580                               vmSymbols::loadClassInternal_name(),
1581                               vmSymbols::string_class_signature(),
1582                               string,
1583                               CHECK_NULL);
1584     } else {
1585       JavaCalls::call_virtual(&result,
1586                               class_loader,
1587                               spec_klass,
1588                               vmSymbols::loadClass_name(),
1589                               vmSymbols::string_class_signature(),
1590                               string,
1591                               CHECK_NULL);
1592     }
1593 
1594     assert(result.get_type() == T_OBJECT, "just checking");
1595     oop obj = (oop) result.get_jobject();
1596 
1597     // Primitive classes return null since forName() can not be
1598     // used to obtain any of the Class objects representing primitives or void
1599     if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
1600       InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(obj));
1601       // For user defined Java class loaders, check that the name returned is
1602       // the same as that requested.  This check is done for the bootstrap
1603       // loader when parsing the class file.
1604       if (class_name == k->name()) {
1605         return k;
1606       }
1607     }
1608     // Class is not found or has the wrong name, return NULL
1609     return NULL;
1610   }
1611 }
1612 


1701 // placeholders()->find_and_add(PlaceholderTable::DEFINE_CLASS),
1702 // you need to find_and_remove it before returning.
1703 // So be careful to not exit with a CHECK_ macro betweeen these calls.
1704 InstanceKlass* SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader,
1705                                                                InstanceKlass* k, TRAPS) {
1706 
1707   Symbol*  name_h = k->name(); // passed in class_name may be null
1708   ClassLoaderData* loader_data = class_loader_data(class_loader);
1709   Dictionary* dictionary = loader_data->dictionary();
1710 
1711   unsigned int d_hash = dictionary->compute_hash(name_h);
1712 
1713   // Hold SD lock around find_class and placeholder creation for DEFINE_CLASS
1714   unsigned int p_hash = placeholders()->compute_hash(name_h);
1715   int p_index = placeholders()->hash_to_index(p_hash);
1716   PlaceholderEntry* probe;
1717 
1718   {
1719     MutexLocker mu(SystemDictionary_lock, THREAD);
1720     // First check if class already defined
1721     if (UnsyncloadClass || (is_parallelDefine(class_loader))) {
1722       InstanceKlass* check = find_class(d_hash, name_h, dictionary);
1723       if (check != NULL) {
1724         return check;
1725       }
1726     }
1727 
1728     // Acquire define token for this class/classloader
1729     probe = placeholders()->find_and_add(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, NULL, THREAD);
1730     // Wait if another thread defining in parallel
1731     // All threads wait - even those that will throw duplicate class: otherwise
1732     // caller is surprised by LinkageError: duplicate, but findLoadedClass fails
1733     // if other thread has not finished updating dictionary
1734     while (probe->definer() != NULL) {
1735       SystemDictionary_lock->wait();
1736     }
1737     // Only special cases allow parallel defines and can use other thread's results
1738     // Other cases fall through, and may run into duplicate defines
1739     // caught by finding an entry in the SystemDictionary
1740     if ((UnsyncloadClass || is_parallelDefine(class_loader)) && (probe->instance_klass() != NULL)) {
1741         placeholders()->find_and_remove(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, THREAD);
1742         SystemDictionary_lock->notify_all();
1743 #ifdef ASSERT
1744         InstanceKlass* check = find_class(d_hash, name_h, dictionary);
1745         assert(check != NULL, "definer missed recording success");
1746 #endif
1747         return probe->instance_klass();
1748     } else {
1749       // This thread will define the class (even if earlier thread tried and had an error)
1750       probe->set_definer(THREAD);
1751     }
1752   }
1753 
1754   define_instance_class(k, THREAD);
1755 
1756   Handle linkage_exception = Handle(); // null handle
1757 
1758   // definer must notify any waiting threads
1759   {
1760     MutexLocker mu(SystemDictionary_lock, THREAD);


   1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


 142   JavaCalls::call_static(&result,
 143                          class_loader_klass,
 144                          vmSymbols::getPlatformClassLoader_name(),
 145                          vmSymbols::void_classloader_signature(),
 146                          CHECK);
 147 
 148   _java_platform_loader = (oop)result.get_jobject();
 149 
 150   CDS_ONLY(SystemDictionaryShared::initialize(CHECK);)
 151 }
 152 
 153 ClassLoaderData* SystemDictionary::register_loader(Handle class_loader, TRAPS) {
 154   if (class_loader() == NULL) return ClassLoaderData::the_null_class_loader_data();
 155   return ClassLoaderDataGraph::find_or_create(class_loader, THREAD);
 156 }
 157 
 158 // ----------------------------------------------------------------------------
 159 // Parallel class loading check
 160 
 161 bool SystemDictionary::is_parallelCapable(Handle class_loader) {
 162   if (class_loader.is_null()) return true;
 163   if (AlwaysLockClassLoader) return false;
 164   return java_lang_ClassLoader::parallelCapable(class_loader());
 165 }
 166 // ----------------------------------------------------------------------------
 167 // ParallelDefineClass flag does not apply to bootclass loader
 168 bool SystemDictionary::is_parallelDefine(Handle class_loader) {
 169    if (class_loader.is_null()) return false;
 170    if (AllowParallelDefineClass && java_lang_ClassLoader::parallelCapable(class_loader())) {
 171      return true;
 172    }
 173    return false;
 174 }
 175 
 176 // Returns true if the passed class loader is the builtin application class loader
 177 // or a custom system class loader. A customer system class loader can be
 178 // specified via -Djava.system.class.loader.
 179 bool SystemDictionary::is_system_class_loader(oop class_loader) {
 180   if (class_loader == NULL) {
 181     return false;
 182   }


 486 
 487     Symbol*  kn = klass->name();
 488     unsigned int d_hash = dictionary->compute_hash(kn);
 489 
 490     MutexLocker mu(SystemDictionary_lock, THREAD);
 491     int d_index = dictionary->hash_to_index(d_hash);
 492     dictionary->add_protection_domain(d_index, d_hash, klass,
 493                                       protection_domain, THREAD);
 494   }
 495 }
 496 
 497 // We only get here if this thread finds that another thread
 498 // has already claimed the placeholder token for the current operation,
 499 // but that other thread either never owned or gave up the
 500 // object lock
 501 // Waits on SystemDictionary_lock to indicate placeholder table updated
 502 // On return, caller must recheck placeholder table state
 503 //
 504 // We only get here if
 505 //  1) custom classLoader, i.e. not bootstrap classloader
 506 //  2) custom classLoader has broken the class loader objectLock

 507 //     so another thread got here in parallel
 508 //
 509 // lockObject must be held.
 510 // Complicated dance due to lock ordering:
 511 // Must first release the classloader object lock to
 512 // allow initial definer to complete the class definition
 513 // and to avoid deadlock
 514 // Reclaim classloader lock object with same original recursion count
 515 // Must release SystemDictionary_lock after notify, since
 516 // class loader lock must be claimed before SystemDictionary_lock
 517 // to prevent deadlocks
 518 //
 519 // The notify allows applications that did an untimed wait() on
 520 // the classloader object lock to not hang.
 521 void SystemDictionary::double_lock_wait(Handle lockObject, TRAPS) {
 522   assert_lock_strong(SystemDictionary_lock);
 523 
 524   bool calledholdinglock
 525       = ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, lockObject);
 526   assert(calledholdinglock,"must hold lock for notify");


 576  if (!class_loader.is_null() && is_parallelCapable(class_loader)) {
 577     MutexLocker mu(SystemDictionary_lock, THREAD);
 578     // Check if classloading completed while we were loading superclass or waiting
 579     return find_class(d_hash, name, dictionary);
 580   }
 581 
 582   // must loop to both handle other placeholder updates
 583   // and spurious notifications
 584   bool super_load_in_progress = true;
 585   PlaceholderEntry* placeholder;
 586   while (super_load_in_progress) {
 587     MutexLocker mu(SystemDictionary_lock, THREAD);
 588     // Check if classloading completed while we were loading superclass or waiting
 589     InstanceKlass* check = find_class(d_hash, name, dictionary);
 590     if (check != NULL) {
 591       // Klass is already loaded, so just return it
 592       return check;
 593     } else {
 594       placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data);
 595       if (placeholder && placeholder->super_load_in_progress() ){

 596         // We only get here if the application has released the
 597         // classloader lock when another thread was in the middle of loading a
 598         // superclass/superinterface for this class, and now
 599         // this thread is also trying to load this class.
 600         // To minimize surprises, the first thread that started to
 601         // load a class should be the one to complete the loading
 602         // with the classfile it initially expected.
 603         // This logic has the current thread wait once it has done
 604         // all the superclass/superinterface loading it can, until
 605         // the original thread completes the class loading or fails
 606         // If it completes we will use the resulting InstanceKlass
 607         // which we will find below in the systemDictionary.
 608         // We also get here for parallel bootstrap classloader
 609         if (class_loader.is_null()) {
 610           SystemDictionary_lock->wait();
 611         } else {
 612           double_lock_wait(lockObject, THREAD);
 613         }
 614       } else {
 615         // If not in SD and not in PH, other thread's load must have failed


 668   Dictionary* dictionary = loader_data->dictionary();
 669   unsigned int d_hash = dictionary->compute_hash(name);
 670 
 671   // Do lookup to see if class already exist and the protection domain
 672   // has the right access
 673   // This call uses find which checks protection domain already matches
 674   // All subsequent calls use find_class, and set has_loaded_class so that
 675   // before we return a result we call out to java to check for valid protection domain
 676   // to allow returning the Klass* and add it to the pd_set if it is valid
 677   {
 678     Klass* probe = dictionary->find(d_hash, name, protection_domain);
 679     if (probe != NULL) return probe;
 680   }
 681 
 682   // Non-bootstrap class loaders will call out to class loader and
 683   // define via jvm/jni_DefineClass which will acquire the
 684   // class loader object lock to protect against multiple threads
 685   // defining the class in parallel by accident.
 686   // This lock must be acquired here so the waiter will find
 687   // any successful result in the SystemDictionary and not attempt
 688   // the define.
 689   // ParallelCapable Classloaders and the bootstrap classloader
 690   // do not acquire lock here.
 691   bool DoObjectLock = true;
 692   if (is_parallelCapable(class_loader)) {
 693     DoObjectLock = false;
 694   }
 695 
 696   unsigned int p_hash = placeholders()->compute_hash(name);
 697   int p_index = placeholders()->hash_to_index(p_hash);
 698 
 699   // Class is not in SystemDictionary so we have to do loading.
 700   // Make sure we are synchronized on the class loader before we proceed
 701   Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
 702   check_loader_lock_contention(lockObject, THREAD);
 703   ObjectLocker ol(lockObject, THREAD, DoObjectLock);
 704 
 705   // Check again (after locking) if class already exist in SystemDictionary
 706   bool class_has_been_loaded   = false;
 707   bool super_load_in_progress  = false;
 708   bool havesupername = false;
 709   InstanceKlass* k = NULL;
 710   PlaceholderEntry* placeholder;


 746 
 747   bool throw_circularity_error = false;
 748   if (!class_has_been_loaded) {
 749     bool load_instance_added = false;
 750 
 751     // add placeholder entry to record loading instance class
 752     // Five cases:
 753     // All cases need to prevent modifying bootclasssearchpath
 754     // in parallel with a classload of same classname
 755     // Redefineclasses uses existence of the placeholder for the duration
 756     // of the class load to prevent concurrent redefinition of not completely
 757     // defined classes.
 758     // case 1. traditional classloaders that rely on the classloader object lock
 759     //   - no other need for LOAD_INSTANCE
 760     // case 2. traditional classloaders that break the classloader object lock
 761     //    as a deadlock workaround. Detection of this case requires that
 762     //    this check is done while holding the classloader object lock,
 763     //    and that lock is still held when calling classloader's loadClass.
 764     //    For these classloaders, we ensure that the first requestor
 765     //    completes the load and other requestors wait for completion.
 766     // case 3. Bootstrap classloader - don't own objectLocker



 767     //    This classloader supports parallelism at the classloader level,
 768     //    but only allows a single load of a class/classloader pair.
 769     //    No performance benefit and no deadlock issues.
 770     // case 4. parallelCapable user level classloaders - without objectLocker
 771     //    Allow parallel classloading of a class/classloader pair
 772 
 773     {
 774       MutexLocker mu(SystemDictionary_lock, THREAD);
 775       if (class_loader.is_null() || !is_parallelCapable(class_loader)) {
 776         PlaceholderEntry* oldprobe = placeholders()->get_entry(p_index, p_hash, name, loader_data);
 777         if (oldprobe) {
 778           // only need check_seen_thread once, not on each loop
 779           // 6341374 java/lang/Instrument with -Xcomp
 780           if (oldprobe->check_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE)) {
 781             throw_circularity_error = true;
 782           } else {
 783             // case 1: traditional: should never see load_in_progress.
 784             while (!class_has_been_loaded && oldprobe && oldprobe->instance_load_in_progress()) {
 785 
 786               // case 4: bootstrap classloader: prevent futile classloading,
 787               // wait on first requestor
 788               if (class_loader.is_null()) {
 789                 SystemDictionary_lock->wait();
 790               } else {
 791               // case 2: traditional with broken classloader lock. wait on first
 792               // requestor.
 793                 double_lock_wait(lockObject, THREAD);
 794               }
 795               // Check if classloading completed while we were waiting
 796               InstanceKlass* check = find_class(d_hash, name, dictionary);
 797               if (check != NULL) {
 798                 // Klass is already loaded, so just return it
 799                 k = check;
 800                 class_has_been_loaded = true;
 801               }
 802               // check if other thread failed to load and cleaned up
 803               oldprobe = placeholders()->get_entry(p_index, p_hash, name, loader_data);
 804             }
 805           }
 806         }
 807       }
 808       // All cases: add LOAD_INSTANCE holding SystemDictionary_lock
 809       // case 4: parallelCapable: allow competing threads to try
 810       // LOAD_INSTANCE in parallel
 811 
 812       if (!throw_circularity_error && !class_has_been_loaded) {
 813         PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, NULL, THREAD);
 814         load_instance_added = true;
 815         // For class loaders that do not acquire the classloader object lock,
 816         // if they did not catch another thread holding LOAD_INSTANCE,
 817         // need a check analogous to the acquire ObjectLocker/find_class
 818         // i.e. now that we hold the LOAD_INSTANCE token on loading this class/CL
 819         // one final check if the load has already completed
 820         // class loaders holding the ObjectLock shouldn't find the class here
 821         InstanceKlass* check = find_class(d_hash, name, dictionary);
 822         if (check != NULL) {
 823         // Klass is already loaded, so return it after checking/adding protection domain
 824           k = check;
 825           class_has_been_loaded = true;
 826         }
 827       }
 828     }
 829 
 830     // must throw error outside of owning lock
 831     if (throw_circularity_error) {
 832       assert(!HAS_PENDING_EXCEPTION && load_instance_added == false,"circularity error cleanup");
 833       ResourceMark rm(THREAD);
 834       THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), name->as_C_string());
 835     }
 836 
 837     if (!class_has_been_loaded) {
 838 
 839       // Do actual loading
 840       k = load_instance_class(name, class_loader, THREAD);
 841 

 842       // If they got a linkageError, check if a parallel class load succeeded.
 843       // If it did, then for bytecode resolution the specification requires
 844       // that we return the same result we did for the other thread, i.e. the
 845       // successfully loaded InstanceKlass
 846       // Should not get here for classloaders that support parallelism
 847       // with the new cleaner mechanism, even with AllowParallelDefineClass
 848       // Bootstrap goes through here to allow for an extra guarantee check
 849       if ((class_loader.is_null())) {
 850         if (k == NULL && HAS_PENDING_EXCEPTION
 851           && PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
 852           MutexLocker mu(SystemDictionary_lock, THREAD);
 853           InstanceKlass* check = find_class(d_hash, name, dictionary);
 854           if (check != NULL) {
 855             // Klass is already loaded, so just use it
 856             k = check;
 857             CLEAR_PENDING_EXCEPTION;
 858             guarantee((!class_loader.is_null()), "dup definition for bootstrap loader?");
 859           }
 860         }
 861       }
 862 
 863       // If everything was OK (no exceptions, no null return value), and
 864       // class_loader is NOT the defining loader, do a little more bookkeeping.
 865       if (!HAS_PENDING_EXCEPTION && k != NULL &&
 866         k->class_loader() != class_loader()) {
 867 
 868         check_constraints(d_hash, k, class_loader, false, THREAD);
 869 


1074 // the class until we have parsed the stream.
1075 
1076 InstanceKlass* SystemDictionary::resolve_from_stream(Symbol* class_name,
1077                                                      Handle class_loader,
1078                                                      Handle protection_domain,
1079                                                      ClassFileStream* st,
1080                                                      TRAPS) {
1081 #if INCLUDE_CDS
1082   ResourceMark rm(THREAD);
1083   if (DumpSharedSpaces && !class_loader.is_null() &&
1084       !UseAppCDS && strcmp(class_name->as_C_string(), "Unnamed") != 0) {
1085     // If AppCDS is not enabled, don't define the class at dump time (except for the "Unnamed"
1086     // class, which is used by MethodHandles).
1087     THROW_MSG_NULL(vmSymbols::java_lang_ClassNotFoundException(), class_name->as_C_string());
1088   }
1089 #endif
1090 
1091   HandleMark hm(THREAD);
1092 
1093   // Classloaders that support parallelism, e.g. bootstrap classloader,
1094   // do not acquire lock here
1095   bool DoObjectLock = true;
1096   if (is_parallelCapable(class_loader)) {
1097     DoObjectLock = false;
1098   }
1099 
1100   ClassLoaderData* loader_data = register_loader(class_loader, CHECK_NULL);
1101 
1102   // Make sure we are synchronized on the class loader before we proceed
1103   Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
1104   check_loader_lock_contention(lockObject, THREAD);
1105   ObjectLocker ol(lockObject, THREAD, DoObjectLock);
1106 
1107   assert(st != NULL, "invariant");
1108 
1109   // Parse the stream and create a klass.
1110   // Note that we do this even though this klass might
1111   // already be present in the SystemDictionary, otherwise we would not
1112   // throw potential ClassFormatErrors.
1113  InstanceKlass* k = NULL;
1114 


1538     PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
1539                                ClassLoader::perf_app_classload_selftime(),
1540                                ClassLoader::perf_app_classload_count(),
1541                                jt->get_thread_stat()->perf_recursion_counts_addr(),
1542                                jt->get_thread_stat()->perf_timers_addr(),
1543                                PerfClassTraceTime::CLASS_LOAD);
1544 
1545     Handle s = java_lang_String::create_from_symbol(class_name, CHECK_NULL);
1546     // Translate to external class name format, i.e., convert '/' chars to '.'
1547     Handle string = java_lang_String::externalize_classname(s, CHECK_NULL);
1548 
1549     JavaValue result(T_OBJECT);
1550 
1551     InstanceKlass* spec_klass = SystemDictionary::ClassLoader_klass();
1552 
1553     // Call public unsynchronized loadClass(String) directly for all class loaders
1554     // for parallelCapable class loaders. JDK >=7, loadClass(String, boolean) will
1555     // acquire a class-name based lock rather than the class loader object lock.
1556     // JDK < 7 already acquire the class loader lock in loadClass(String, boolean),
1557     // so the call to loadClassInternal() was not required.





















1558     JavaCalls::call_virtual(&result,
1559                             class_loader,
1560                             spec_klass,
1561                             vmSymbols::loadClass_name(),
1562                             vmSymbols::string_class_signature(),
1563                             string,
1564                             CHECK_NULL);

1565 
1566     assert(result.get_type() == T_OBJECT, "just checking");
1567     oop obj = (oop) result.get_jobject();
1568 
1569     // Primitive classes return null since forName() can not be
1570     // used to obtain any of the Class objects representing primitives or void
1571     if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
1572       InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(obj));
1573       // For user defined Java class loaders, check that the name returned is
1574       // the same as that requested.  This check is done for the bootstrap
1575       // loader when parsing the class file.
1576       if (class_name == k->name()) {
1577         return k;
1578       }
1579     }
1580     // Class is not found or has the wrong name, return NULL
1581     return NULL;
1582   }
1583 }
1584 


1673 // placeholders()->find_and_add(PlaceholderTable::DEFINE_CLASS),
1674 // you need to find_and_remove it before returning.
1675 // So be careful to not exit with a CHECK_ macro betweeen these calls.
1676 InstanceKlass* SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader,
1677                                                                InstanceKlass* k, TRAPS) {
1678 
1679   Symbol*  name_h = k->name(); // passed in class_name may be null
1680   ClassLoaderData* loader_data = class_loader_data(class_loader);
1681   Dictionary* dictionary = loader_data->dictionary();
1682 
1683   unsigned int d_hash = dictionary->compute_hash(name_h);
1684 
1685   // Hold SD lock around find_class and placeholder creation for DEFINE_CLASS
1686   unsigned int p_hash = placeholders()->compute_hash(name_h);
1687   int p_index = placeholders()->hash_to_index(p_hash);
1688   PlaceholderEntry* probe;
1689 
1690   {
1691     MutexLocker mu(SystemDictionary_lock, THREAD);
1692     // First check if class already defined
1693     if (is_parallelDefine(class_loader)) {
1694       InstanceKlass* check = find_class(d_hash, name_h, dictionary);
1695       if (check != NULL) {
1696         return check;
1697       }
1698     }
1699 
1700     // Acquire define token for this class/classloader
1701     probe = placeholders()->find_and_add(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, NULL, THREAD);
1702     // Wait if another thread defining in parallel
1703     // All threads wait - even those that will throw duplicate class: otherwise
1704     // caller is surprised by LinkageError: duplicate, but findLoadedClass fails
1705     // if other thread has not finished updating dictionary
1706     while (probe->definer() != NULL) {
1707       SystemDictionary_lock->wait();
1708     }
1709     // Only special cases allow parallel defines and can use other thread's results
1710     // Other cases fall through, and may run into duplicate defines
1711     // caught by finding an entry in the SystemDictionary
1712     if (is_parallelDefine(class_loader) && (probe->instance_klass() != NULL)) {
1713         placeholders()->find_and_remove(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, THREAD);
1714         SystemDictionary_lock->notify_all();
1715 #ifdef ASSERT
1716         InstanceKlass* check = find_class(d_hash, name_h, dictionary);
1717         assert(check != NULL, "definer missed recording success");
1718 #endif
1719         return probe->instance_klass();
1720     } else {
1721       // This thread will define the class (even if earlier thread tried and had an error)
1722       probe->set_definer(THREAD);
1723     }
1724   }
1725 
1726   define_instance_class(k, THREAD);
1727 
1728   Handle linkage_exception = Handle(); // null handle
1729 
1730   // definer must notify any waiting threads
1731   {
1732     MutexLocker mu(SystemDictionary_lock, THREAD);


< prev index next >