src/hotspot/share/classfile/systemDictionary.cpp
changeset 49037 dc68aeea4840
parent 48826 c4d9d1b08e2e
child 49332 cd21aff24069
equal deleted inserted replaced
49036:bc92debe57e4 49037:dc68aeea4840
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
   104 InstanceKlass*      SystemDictionary::_box_klasses[T_VOID+1]      =  { NULL /*, NULL...*/ };
   104 InstanceKlass*      SystemDictionary::_box_klasses[T_VOID+1]      =  { NULL /*, NULL...*/ };
   105 
   105 
   106 oop         SystemDictionary::_java_system_loader         =  NULL;
   106 oop         SystemDictionary::_java_system_loader         =  NULL;
   107 oop         SystemDictionary::_java_platform_loader       =  NULL;
   107 oop         SystemDictionary::_java_platform_loader       =  NULL;
   108 
   108 
   109 bool        SystemDictionary::_has_loadClassInternal      =  false;
       
   110 bool        SystemDictionary::_has_checkPackageAccess     =  false;
   109 bool        SystemDictionary::_has_checkPackageAccess     =  false;
   111 
   110 
   112 // lazily initialized klass variables
   111 // lazily initialized klass variables
   113 InstanceKlass* volatile SystemDictionary::_abstract_ownable_synchronizer_klass = NULL;
   112 InstanceKlass* volatile SystemDictionary::_abstract_ownable_synchronizer_klass = NULL;
   114 
   113 
   157 
   156 
   158 // ----------------------------------------------------------------------------
   157 // ----------------------------------------------------------------------------
   159 // Parallel class loading check
   158 // Parallel class loading check
   160 
   159 
   161 bool SystemDictionary::is_parallelCapable(Handle class_loader) {
   160 bool SystemDictionary::is_parallelCapable(Handle class_loader) {
   162   if (UnsyncloadClass || class_loader.is_null()) return true;
   161   if (class_loader.is_null()) return true;
   163   if (AlwaysLockClassLoader) return false;
   162   if (AlwaysLockClassLoader) return false;
   164   return java_lang_ClassLoader::parallelCapable(class_loader());
   163   return java_lang_ClassLoader::parallelCapable(class_loader());
   165 }
   164 }
   166 // ----------------------------------------------------------------------------
   165 // ----------------------------------------------------------------------------
   167 // ParallelDefineClass flag does not apply to bootclass loader
   166 // ParallelDefineClass flag does not apply to bootclass loader
   501 // Waits on SystemDictionary_lock to indicate placeholder table updated
   500 // Waits on SystemDictionary_lock to indicate placeholder table updated
   502 // On return, caller must recheck placeholder table state
   501 // On return, caller must recheck placeholder table state
   503 //
   502 //
   504 // We only get here if
   503 // We only get here if
   505 //  1) custom classLoader, i.e. not bootstrap classloader
   504 //  1) custom classLoader, i.e. not bootstrap classloader
   506 //  2) UnsyncloadClass not set
   505 //  2) custom classLoader has broken the class loader objectLock
   507 //  3) custom classLoader has broken the class loader objectLock
       
   508 //     so another thread got here in parallel
   506 //     so another thread got here in parallel
   509 //
   507 //
   510 // lockObject must be held.
   508 // lockObject must be held.
   511 // Complicated dance due to lock ordering:
   509 // Complicated dance due to lock ordering:
   512 // Must first release the classloader object lock to
   510 // Must first release the classloader object lock to
   592       // Klass is already loaded, so just return it
   590       // Klass is already loaded, so just return it
   593       return check;
   591       return check;
   594     } else {
   592     } else {
   595       placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data);
   593       placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data);
   596       if (placeholder && placeholder->super_load_in_progress() ){
   594       if (placeholder && placeholder->super_load_in_progress() ){
   597         // Before UnsyncloadClass:
       
   598         // We only get here if the application has released the
   595         // We only get here if the application has released the
   599         // classloader lock when another thread was in the middle of loading a
   596         // classloader lock when another thread was in the middle of loading a
   600         // superclass/superinterface for this class, and now
   597         // superclass/superinterface for this class, and now
   601         // this thread is also trying to load this class.
   598         // this thread is also trying to load this class.
   602         // To minimize surprises, the first thread that started to
   599         // To minimize surprises, the first thread that started to
   685   // define via jvm/jni_DefineClass which will acquire the
   682   // define via jvm/jni_DefineClass which will acquire the
   686   // class loader object lock to protect against multiple threads
   683   // class loader object lock to protect against multiple threads
   687   // defining the class in parallel by accident.
   684   // defining the class in parallel by accident.
   688   // This lock must be acquired here so the waiter will find
   685   // This lock must be acquired here so the waiter will find
   689   // any successful result in the SystemDictionary and not attempt
   686   // any successful result in the SystemDictionary and not attempt
   690   // the define
   687   // the define.
   691   // ParallelCapable Classloaders and the bootstrap classloader,
   688   // ParallelCapable Classloaders and the bootstrap classloader
   692   // or all classloaders with UnsyncloadClass do not acquire lock here
   689   // do not acquire lock here.
   693   bool DoObjectLock = true;
   690   bool DoObjectLock = true;
   694   if (is_parallelCapable(class_loader)) {
   691   if (is_parallelCapable(class_loader)) {
   695     DoObjectLock = false;
   692     DoObjectLock = false;
   696   }
   693   }
   697 
   694 
   763     //    as a deadlock workaround. Detection of this case requires that
   760     //    as a deadlock workaround. Detection of this case requires that
   764     //    this check is done while holding the classloader object lock,
   761     //    this check is done while holding the classloader object lock,
   765     //    and that lock is still held when calling classloader's loadClass.
   762     //    and that lock is still held when calling classloader's loadClass.
   766     //    For these classloaders, we ensure that the first requestor
   763     //    For these classloaders, we ensure that the first requestor
   767     //    completes the load and other requestors wait for completion.
   764     //    completes the load and other requestors wait for completion.
   768     // case 3. UnsyncloadClass - don't use objectLocker
   765     // case 3. Bootstrap classloader - don't own 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,
   766     //    This classloader supports parallelism at the classloader level,
   773     //    but only allows a single load of a class/classloader pair.
   767     //    but only allows a single load of a class/classloader pair.
   774     //    No performance benefit and no deadlock issues.
   768     //    No performance benefit and no deadlock issues.
   775     // case 5. parallelCapable user level classloaders - without objectLocker
   769     // case 4. parallelCapable user level classloaders - without objectLocker
   776     //    Allow parallel classloading of a class/classloader pair
   770     //    Allow parallel classloading of a class/classloader pair
   777 
   771 
   778     {
   772     {
   779       MutexLocker mu(SystemDictionary_lock, THREAD);
   773       MutexLocker mu(SystemDictionary_lock, THREAD);
   780       if (class_loader.is_null() || !is_parallelCapable(class_loader)) {
   774       if (class_loader.is_null() || !is_parallelCapable(class_loader)) {
   786             throw_circularity_error = true;
   780             throw_circularity_error = true;
   787           } else {
   781           } else {
   788             // case 1: traditional: should never see load_in_progress.
   782             // case 1: traditional: should never see load_in_progress.
   789             while (!class_has_been_loaded && oldprobe && oldprobe->instance_load_in_progress()) {
   783             while (!class_has_been_loaded && oldprobe && oldprobe->instance_load_in_progress()) {
   790 
   784 
   791               // case 4: bootstrap classloader: prevent futile classloading,
   785               // case 3: bootstrap classloader: prevent futile classloading,
   792               // wait on first requestor
   786               // wait on first requestor
   793               if (class_loader.is_null()) {
   787               if (class_loader.is_null()) {
   794                 SystemDictionary_lock->wait();
   788                 SystemDictionary_lock->wait();
   795               } else {
   789               } else {
   796               // case 2: traditional with broken classloader lock. wait on first
   790               // case 2: traditional with broken classloader lock. wait on first
   809             }
   803             }
   810           }
   804           }
   811         }
   805         }
   812       }
   806       }
   813       // All cases: add LOAD_INSTANCE holding SystemDictionary_lock
   807       // All cases: add LOAD_INSTANCE holding SystemDictionary_lock
   814       // case 3: UnsyncloadClass || case 5: parallelCapable: allow competing threads to try
   808       // case 4: parallelCapable: allow competing threads to try
   815       // LOAD_INSTANCE in parallel
   809       // LOAD_INSTANCE in parallel
   816 
   810 
   817       if (!throw_circularity_error && !class_has_been_loaded) {
   811       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);
   812         PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, NULL, THREAD);
   819         load_instance_added = true;
   813         load_instance_added = true;
   842     if (!class_has_been_loaded) {
   836     if (!class_has_been_loaded) {
   843 
   837 
   844       // Do actual loading
   838       // Do actual loading
   845       k = load_instance_class(name, class_loader, THREAD);
   839       k = load_instance_class(name, class_loader, THREAD);
   846 
   840 
   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
   841       // If everything was OK (no exceptions, no null return value), and
   870       // class_loader is NOT the defining loader, do a little more bookkeeping.
   842       // class_loader is NOT the defining loader, do a little more bookkeeping.
   871       if (!HAS_PENDING_EXCEPTION && k != NULL &&
   843       if (!HAS_PENDING_EXCEPTION && k != NULL &&
   872         k->class_loader() != class_loader()) {
   844         k->class_loader() != class_loader()) {
   873 
   845 
  1095 #endif
  1067 #endif
  1096 
  1068 
  1097   HandleMark hm(THREAD);
  1069   HandleMark hm(THREAD);
  1098 
  1070 
  1099   // Classloaders that support parallelism, e.g. bootstrap classloader,
  1071   // Classloaders that support parallelism, e.g. bootstrap classloader,
  1100   // or all classloaders with UnsyncloadClass do not acquire lock here
  1072   // do not acquire lock here
  1101   bool DoObjectLock = true;
  1073   bool DoObjectLock = true;
  1102   if (is_parallelCapable(class_loader)) {
  1074   if (is_parallelCapable(class_loader)) {
  1103     DoObjectLock = false;
  1075     DoObjectLock = false;
  1104   }
  1076   }
  1105 
  1077 
  1554 
  1526 
  1555     JavaValue result(T_OBJECT);
  1527     JavaValue result(T_OBJECT);
  1556 
  1528 
  1557     InstanceKlass* spec_klass = SystemDictionary::ClassLoader_klass();
  1529     InstanceKlass* spec_klass = SystemDictionary::ClassLoader_klass();
  1558 
  1530 
  1559     // Call public unsynchronized loadClass(String) directly for all class loaders
  1531     // Call public unsynchronized loadClass(String) directly for all class loaders.
  1560     // for parallelCapable class loaders. JDK >=7, loadClass(String, boolean) will
  1532     // For parallelCapable class loaders, JDK >=7, loadClass(String, boolean) will
  1561     // acquire a class-name based lock rather than the class loader object lock.
  1533     // 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),
  1534     // JDK < 7 already acquire the class loader lock in loadClass(String, boolean).
  1563     // so the call to loadClassInternal() was not required.
  1535     JavaCalls::call_virtual(&result,
  1564     //
  1536                             class_loader,
  1565     // UnsyncloadClass flag means both call loadClass(String) and do
  1537                             spec_klass,
  1566     // not acquire the class loader lock even for class loaders that are
  1538                             vmSymbols::loadClass_name(),
  1567     // not parallelCapable. This was a risky transitional
  1539                             vmSymbols::string_class_signature(),
  1568     // flag for diagnostic purposes only. It is risky to call
  1540                             string,
  1569     // custom class loaders without synchronization.
  1541                             CHECK_NULL);
  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 
  1542 
  1594     assert(result.get_type() == T_OBJECT, "just checking");
  1543     assert(result.get_type() == T_OBJECT, "just checking");
  1595     oop obj = (oop) result.get_jobject();
  1544     oop obj = (oop) result.get_jobject();
  1596 
  1545 
  1597     // Primitive classes return null since forName() can not be
  1546     // Primitive classes return null since forName() can not be
  1716   PlaceholderEntry* probe;
  1665   PlaceholderEntry* probe;
  1717 
  1666 
  1718   {
  1667   {
  1719     MutexLocker mu(SystemDictionary_lock, THREAD);
  1668     MutexLocker mu(SystemDictionary_lock, THREAD);
  1720     // First check if class already defined
  1669     // First check if class already defined
  1721     if (UnsyncloadClass || (is_parallelDefine(class_loader))) {
  1670     if (is_parallelDefine(class_loader)) {
  1722       InstanceKlass* check = find_class(d_hash, name_h, dictionary);
  1671       InstanceKlass* check = find_class(d_hash, name_h, dictionary);
  1723       if (check != NULL) {
  1672       if (check != NULL) {
  1724         return check;
  1673         return check;
  1725       }
  1674       }
  1726     }
  1675     }
  1735       SystemDictionary_lock->wait();
  1684       SystemDictionary_lock->wait();
  1736     }
  1685     }
  1737     // Only special cases allow parallel defines and can use other thread's results
  1686     // Only special cases allow parallel defines and can use other thread's results
  1738     // Other cases fall through, and may run into duplicate defines
  1687     // Other cases fall through, and may run into duplicate defines
  1739     // caught by finding an entry in the SystemDictionary
  1688     // caught by finding an entry in the SystemDictionary
  1740     if ((UnsyncloadClass || is_parallelDefine(class_loader)) && (probe->instance_klass() != NULL)) {
  1689     if (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);
  1690         placeholders()->find_and_remove(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, THREAD);
  1742         SystemDictionary_lock->notify_all();
  1691         SystemDictionary_lock->notify_all();
  1743 #ifdef ASSERT
  1692 #ifdef ASSERT
  1744         InstanceKlass* check = find_class(d_hash, name_h, dictionary);
  1693         InstanceKlass* check = find_class(d_hash, name_h, dictionary);
  1745         assert(check != NULL, "definer missed recording success");
  1694         assert(check != NULL, "definer missed recording success");
  2172   _box_klasses[T_INT]     = WK_KLASS(Integer_klass);
  2121   _box_klasses[T_INT]     = WK_KLASS(Integer_klass);
  2173   _box_klasses[T_LONG]    = WK_KLASS(Long_klass);
  2122   _box_klasses[T_LONG]    = WK_KLASS(Long_klass);
  2174   //_box_klasses[T_OBJECT]  = WK_KLASS(object_klass);
  2123   //_box_klasses[T_OBJECT]  = WK_KLASS(object_klass);
  2175   //_box_klasses[T_ARRAY]   = WK_KLASS(object_klass);
  2124   //_box_klasses[T_ARRAY]   = WK_KLASS(object_klass);
  2176 
  2125 
  2177   { // Compute whether we should use loadClass or loadClassInternal when loading classes.
       
  2178     Method* method = InstanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::loadClassInternal_name(), vmSymbols::string_class_signature());
       
  2179     _has_loadClassInternal = (method != NULL);
       
  2180   }
       
  2181   { // Compute whether we should use checkPackageAccess or NOT
  2126   { // Compute whether we should use checkPackageAccess or NOT
  2182     Method* method = InstanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::checkPackageAccess_name(), vmSymbols::class_protectiondomain_signature());
  2127     Method* method = InstanceKlass::cast(ClassLoader_klass())->find_method(vmSymbols::checkPackageAccess_name(), vmSymbols::class_protectiondomain_signature());
  2183     _has_checkPackageAccess = (method != NULL);
  2128     _has_checkPackageAccess = (method != NULL);
  2184   }
  2129   }
  2185 }
  2130 }